嗯,我试着去理解和解读导致它的原因,但我就是不明白:

在我的代码中有这样的代码:

 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.

是什么导致了这样的行为或者我如何检查这样的事情?


当前回答

列出Eclipse Navigator模式下的所有jar文件 验证所有jar文件都是二进制模式

其他回答

我有一个java.lang.reflect.InvocationTargetException错误,来自在我的类的try / catch块内部的外部类中调用记录器对象的语句。

在Eclipse调试器中遍历代码并将鼠标悬停在记录器语句上,我看到记录器对象为空(一些外部常数需要在类的最顶部实例化)。

我也面临着同样的问题。我使用e.getCause(). getcause(),然后我发现这是因为我传递了错误的参数。在获取其中一个参数的值时出现nullPointerException。 希望这对你有所帮助。

来自Method.invoke()的Javadoc

如果底层方法抛出异常,则抛出:InvocationTargetException。

如果调用的方法抛出异常,则抛出此异常。

通过调用带有反射的方法,您增加了一个额外的抽象级别。反射层将任何异常包装在InvocationTargetException中,这使您可以区分实际由反射调用中的失败(例如,可能您的参数列表无效)引起的异常和所调用的方法中的失败。

只需在InvocationTargetException中展开原因,就会得到原始的原因。

我这么做之后,错误就消失了 Clean->执行xDoclet->执行xPackaging。

在我的工作空间,在月蚀中。