user550413 user550413 - 1 year ago 88
Java Question

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 somewhere in my code this:

} catch(AssertionError e){
} catch(Exception e){

Thing is that, when it tries to invoke some method it throws
instead of some other expected exception (specifically
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
and it really threw
as expected. Yet when going up it
somehow changes to
and in the code above
catch(Exception e)

e is
and not

as expected.

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download