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?
What Causes InvocationTargetException. The InvocationTargetException occurs mainly when working with the Java reflection API to invoke a method or constructor, which throws an exception.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With