How can I catch a NoSuchMethodException?

There are some incompatible changes in the code that I am dependent on. So I want to catch a NoSuchMethodException to log additional information about the problem

When I use this:

try{ do.something(); }catch(NoSuchMethodException e){ System.out.println("!"); } 

I get this error "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the body of the try statement"

I tried to catch also java.lang.RuntimeException and check if it was NoSuchMethod, but it did not work.

Reflection will cause delays in work and will not want to use it.

Any ideas?

+4
source share
7 answers

You are confused by a NoSuchMethodException, which is raised only when a method is called that does not exist using reflection and NoSuchMethodError, which can occur when the method is called directly, when this method exists at compile time and does not exist at run time. This usually happens when using different versions of some external library during compilation and runtime.

Bottom line: catch NoSuchMethodError

+6
source

Catch NoSuchMethodError . However, this seems like an ugly workaround, and I would recommend just allowing incompatibilities.

+5
source

NoSuchMethod should mean that you are trying to access a method that does not exist, probably at runtime. Best of all, if you fix it, rather than trying to catch the Exception.

It is better to place the method where it should be such that you do not have logical errors, such as incorrect output, etc.

+1
source

I would recommend you go @matt b to answer. Use NoSuchMethodError.

But, if you want to catch a NoSuchMethodException at all, you will have to explicitly use it,

 throw new NoSuchMethodException(); 
+1
source

Inaccessible catch block for NoSuchMethodException exception. This exception is never thrown from the body of a try statement.

As reported by the IDE / Compiler. In the try test, a NoSuchMethodException is never thrown. This means that none of the methods you call are declared as:

 public void doSomething() throws NoSuchMethodException 

NoSuchMethodException was literally thrown NoSuchMethodException in the try body:

 throw new NoSuchMethodException(); 
+1
source

EDIT . This serves as a general decision on how to declare checked exceptions. As others have pointed out, you should catch a NoSuchMethodError or even an IncompatibleClassChangeError , because that is what will be thrown at runtime.


Since you want to associate yourself with another version of this library, and not compile it (it is not recommended by itself, but I do not judge), you need to convince the compiler that everything is in order.

One way to do this in this case is to create a static helper declaring an exception:

 class Util { public static void unsafeApiCall() throws NoSuchMethodException { // if (false) prevents a compilation error if (false) throw new NoSuchMethodException(); }; } 

use as

 try { Util.unsafeApiCall(); do.something(); } catch(NoSuchMethodException e2) { System.out.println("!"); } 

It also stands out visually, which is good.


EDIT2 . This can be inconvenient if you select excluded exceptions without declaring them, as described here .

+1
source

Firstly, I strongly disagree with all the people who say that you will catch Bugs. Do not do this. Errors should be left alone to bubble up to any exception handler for the entire application that your application has. If your application does this, then all you need to do to get your evidence of incompatibility is grep your log file.

Also, do not check the code using reflection until you profile it and figure out how long it will take. Usually guesses where the slowdown is ultimately wrong. You should never optimize anything until you take the measurements and demonstrate that this is a bottleneck.

Dig the do.something code, decompile it if you don't have a source, and see what it actually chooses. Perhaps if an exception is thrown, it can be wrapped in another exception. Make sure that everything that the method throws is not eaten (including its own logging).

+1
source

All Articles