Alyoshak Alyoshak -4 years ago 237
Java Question

How to fix: java method in Anonymous class derived from <class> clashes with java method

Android Studio is giving me a "[java method] in Anonymous class derived from [java class] clashes with [java method]" on both of the "throws CertificateException" parts of the following code. The error adds: "overridden method does not throw javax.security.cert.CertificateException" How to satisfy the compiler?

Frankly, I am just as interested in understanding the general cause for this as the solution itself. Notice the two commented out lines. I added them to see if it would prevent the error, hoping that the cause was simply that there was no code actually throwing the specified exception. Did not work.

I would so appreciate it if the correct answer could explain the precise reason for this (I don't why this is an "anonymous class"). This exact code is used in a lot of examples online and many of them seem to use it in a similar way (see here).

X509TrustManager trustManager = new X509TrustManager()
{
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException
{
//if (authType == null)
// throw new javax.security.cert.CertificateException();
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException
{
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return new java.security.cert.X509Certificate[]{};
}
};

Answer Source

You cannot throw CertificateException because the method defined in X509TrustManager does throw java.security.cert.CertificateException, which is not the same as the one you are trying to throw.

javax.security.cert.CertificateException is different from java.security.cert.CertificateException.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download