什么会导致java.lang.reflect.InvocationTargetException?

本文翻译自: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 ). 事实是,当它尝试调用某些方法时,它将引发InvocationTargetException而不是其他预期的异常(特别是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. 我实际上知道调用了什么方法,所以我直接转到此方法代码,并为该行添加了一个try-catch块,该块假定抛出ArrayIndexOutOfBoundsException ,并且确实按预期抛出了ArrayIndexOutOfBoundsException 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. 然而,当它上升时,它会以某种方式更改为InvocationTargetException并且在上面的代码中catch(Exception e) e是InvocationTargetException而不是预期的ArrayIndexOutOfBoundsException

What could cause such a behavior or how can I check such a thing? 是什么原因导致这种行为的?我该如何检查?


#1楼

参考:https://stackoom.com/question/PGGN/什么会导致java-lang-reflect-InvocationTargetException


#2楼

InvocationTargetException上使用getCause()方法来检索原始异常。


#3楼

  1. List all jar files from the Eclipse Navigator mode 列出Eclipse Navigator模式下的所有jar文件
  2. Verify that all the jar files are in binary mode 验证所有的jar文件都处于二进制模式

#4楼

This describes something like, 描述了类似的东西,

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. InvocationTargetException是一个已检查的异常,它包装了由调用的方法或构造函数引发的异常。 As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. 从版本1.4开始,已对该异常进行了改进,以符合通用异常链机制。 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." 现在,在构造时提供并通过getTargetException()方法访问的“目标异常”被称为原因,可以通过Throwable.getCause()方法以及上述“旧方法”进行访问。


#5楼

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. 因此,如果已由反射API调用的方法抛出异常(例如运行时异常),则反射API会将异常包装到InvocationTargetException中。


#6楼

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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值