Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause java.lang.reflect.InvocationTargetException?

Well, I've tried to understand and read what could cause it but I just can't get it:

I have this somewhere in my code:

 try{  ..  m.invoke(testObject);  ..  } catch(AssertionError e){  ...  } catch(Exception e){  ..  } 

Thing is that, when it tries to invoke some method it throws InvocationTargetException instead of some other expected exception (specifically ArrayIndexOutOfBoundsException). As I actually know what method is invoked I went straight to this method code and added a try-catch block for the line that suppose to throw ArrayIndexOutOfBoundsException and it really threw ArrayIndexOutOfBoundsException as expected. Yet when going up it somehow changes to InvocationTargetException and in the code above catch(Exception e) e is InvocationTargetException and not ArrayIndexOutOfBoundsException as expected.

What could cause such a behavior or how can I check such a thing?

like image 562
user550413 Avatar asked May 16 '11 16:05

user550413


People also ask

Why does Java Lang reflect InvocationTargetException?

What Causes InvocationTargetException. The InvocationTargetException occurs mainly when working with the Java reflection API to invoke a method or constructor, which throws an exception.

How do I fix Java Lang reflect InvocationTargetException in Java?

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 catch Java Lang reflect InvocationTargetException?

The InvocationTargetException is caused by the invoked method, which throws an exception. The underlying exception can be found using the getCause() method. Therefore, it is necessary to find the actual exception and resolve it to resolve the InvocationTargetException.

Is RuntimeException subclass of exception?

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.


2 Answers

You've added an extra level of abstraction by calling the method with reflection. The reflection layer wraps any exception in an InvocationTargetException, which lets you tell the difference between an exception actually caused by a failure in the reflection call (maybe your argument list wasn't valid, for example) and a failure within the method called.

Just unwrap the cause within the InvocationTargetException and you'll get to the original one.

like image 179
Jon Skeet Avatar answered Oct 02 '22 16:10

Jon Skeet


The exception is thrown if

InvocationTargetException - if the underlying method throws an exception.

So if the method, that has been invoked with reflection API, throws an exception (runtime exception for example), the reflection API will wrap the exception into an InvocationTargetException.

like image 22
Andreas Dolk Avatar answered Oct 02 '22 17:10

Andreas Dolk