Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a method which throws an Exception using Java Reflection?

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.

like image 487
cleroo Avatar asked Jul 12 '12 12:07

cleroo


People also ask

How do you call a method that throws an exception in Java?

The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown.

How do you handle Java Lang reflect InvocationTargetException?

Since the InvocationTargetException is caused by another exception thrown by the invoked method, the underlying exception can be found using the getCause() method. Therefore, resolving the InvocationTargetException error equates to finding the actual exception and resolving it.

How do you invoke a method in Java?

To call a method in Java, write the method's name followed by two parentheses () and a semicolon; The process of method calling is simple.

What does Java Lang reflect InvocationTargetException mean?

java.lang.reflect.InvocationTargetException. InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism.


2 Answers

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 {
      ...
   }
}
like image 146
Francisco Spaeth Avatar answered Sep 20 '22 14:09

Francisco Spaeth


One way to do it:

try { myMethod.invoke(null, myParam); }
catch (InvocationTargetException e) { 
  try { throw e.getCause(); }
  catch (MyCustomException e) { ...}
  catch (MyOtherException e) { ...}
}
like image 37
Marko Topolnik Avatar answered Sep 22 '22 14:09

Marko Topolnik