可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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
.
回答3:
Use the getCause()
method on the InvocationTargetException
to retrieve the original exception.
回答4:
From the Javadoc of Method.invoke()
Throws: InvocationTargetException - if the underlying method throws an exception.
This exception is thrown if the method called threw an exception.
回答5:
This will print the exact line of code in the specific method, which when invoked, raised the exception:
try {
// try code
..
m.invoke(testObject);
..
} catch (InvocationTargetException e) {
// Answer:
e.getCause().printStackTrace();
} catch (Exception e) {
// generic exception handling
e.printStackTrace();
}
回答6:
That InvocationTargetException
is probably wrapping up your ArrayIndexOutOfBoundsException
. There is no telling upfront when using reflection what that method can throw -- so rather than using a throws Exception
approach, all the exceptions are being caught and wrapped up in InvocationTargetException
.
回答7:
This describes something like,
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. The \"target exception\" that is
provided at construction time and accessed via the
getTargetException() method is now known as the cause, and may be
accessed via the Throwable.getCause() method, as well as the
aforementioned \"legacy method.\"
回答8:
You can compare with the original exception Class using getCause() method like this :
try{
...
} catch(Exception e){
if(e.getCause().getClass().equals(AssertionError.class)){
// handle your exception 1
} else {
// handle the rest of the world exception
}
}
回答9:
I had a java.lang.reflect.InvocationTargetException
error from a statement calling a logger object in an external class
inside a try
/ catch
block in my class
.
Stepping through the code in the Eclipse debugger & hovering the mouse over the logger statement I saw the logger object
was null
(some external constants needed to be instantiated at the very top of my class
).
回答10:
This exception is thrown if the underlying method(method called using Reflection) throws an exception.
So if the method, that has been invoked by reflection API, throws an exception (as for example runtime exception), the reflection API will wrap the exception into an InvocationTargetException.
回答11:
I was facing the same problem. I used e.getCause().getCause() then I found that it was because of wrong parameters I was passing. There was nullPointerException in fetching the value of one of the parameters.
Hope this will help you.
回答12:
- List all jar files from the Eclipse Navigator mode
- Verify that all the jar files are in binary mode
回答13:
The error vanished after I did
Clean->Run xDoclet->Run xPackaging.
In my workspace, in ecllipse.