23

I would like to invoke a method, using Java Reflection.

The problem is that this method (which I wrote) throws an Exception (I created a myCustomException). When I add a try/catch clause, I can't run my project, because Eclipse says "the catch clause is unreachable".

Here is when I try to invoke myMethod in the class MyClass :

270.    myMethod.invoke(null, myParam); // NB : null because myMethod is static

When myMethod does not throw a MyCustomException, eveything is fine. But when it throws a MyCustomException, I get this error message :

Let's say I try to invoke fooMethod(), which is in the class BarClass(), and :

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.tests.MyClass.test5(270)
    at com.tests.MyClass.main(297)
Caused by: com.tests.MyCustomException
    at com.tests.barClass.fooMethod(BarClass.java:129)
    ... 6 more

Is it even possible ? Thanks for help anyway.

cleroo
  • 1,145
  • 2
  • 11
  • 17

3 Answers3

33

You can get the cause of it that would be the original exception.

InvocationTargetException.getCause();

From documentation:

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/InvocationTargetException.html

In your catch block, you could check if exception is from the type you expect and handle it.

One simple approach would be:

try {
   ...
} catch (InvocationTargetException ite) {
   if (ite.getCause() instanceof SomeExceptionType) {
      ...
   } else {
      ...
   }
}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • Thanks ! It works. But I can't get the message from my MyCustomException, with `getCause().getMessage()`, even if I created my MyCustomException like this : `new MyCustomException("the message")`. – cleroo Jul 12 '12 at 12:57
  • I think this could be due your specific implementation, could you debug and check if the exception is really holding the message? – Francisco Spaeth Jul 12 '12 at 13:00
  • I succeeded with Marko Topolnik 's way. Thank you anyway. – cleroo Jul 12 '12 at 13:04
  • OP, Francisco's approach and mine are equivalent and should either both give you the message, or none, depending on factors not related to the code we are showing. The only advantage of my approach is not having to go through the if-else parade. – Marko Topolnik Jul 12 '12 at 13:07
  • Actually it didn't work either ! I only get `null`when I do `getMessage()`, which is strange because the same class works perfectly in an other context without reflection. – cleroo Jul 12 '12 at 13:08
  • Sorry, it was my fault ! You'll see my error quickly, here was my constructor : `public FormulaireException(String string) { super(); }`. `I should have done :super(string);` – cleroo Jul 12 '12 at 13:14
10

One way to do it:

try { myMethod.invoke(null, myParam); }
catch (InvocationTargetException e) { 
  try { throw e.getCause(); }
  catch (MyCustomException e) { ...}
  catch (MyOtherException e) { ...}
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • It works. It was my fault, in my MyCustomException, the constructor was not set correctly : I didn't pass the message ! Stupid error – cleroo Jul 12 '12 at 13:19
1

If you are trying to add a catch clause in the method that executes myMethod.invoke(null, myParam), then that's obviously not the right way of doing it. In this case you're invoking the method via reflection and this is not the place to be catching the exception, as the invoke method throws other exceptions. Once you invoke the method that throws the exception, if there is an exception, it will get thrown and wrapped in an InvocationTargetException, if I recall correctly.

Check the last part of this explanation concerning the InvocationTargetException.

carlspring
  • 31,231
  • 29
  • 115
  • 197