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

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

 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.

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


当前回答

它描述的是,

InvocationTargetException是包装对象的受控异常 由被调用的方法或构造函数引发的异常。截至发布 1.4,此异常已进行了改进,以符合通用异常链接机制。即“目标异常” 在施工时提供,并通过 getTargetException()方法现在被认为是原因,并且可能是 可以通过Throwable.getCause()方法访问 前面提到的“遗留方法”。

其他回答

问题还可能是targetSdkVersion升级了,并且您使用了已弃用的Gradle清单特性。尝试再次降低targetSdkVersion,看看它是否有效。在我的例子中,它是targetSdkVersion 31 -> 30

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

那个InvocationTargetException可能正在封装你的ArrayIndexOutOfBoundsException。在使用反射时,没有预先说明该方法可以抛出什么——因此,与使用抛出异常方法不同,所有异常都被捕获并封装在InvocationTargetException中。

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

它描述的是,

InvocationTargetException是包装对象的受控异常 由被调用的方法或构造函数引发的异常。截至发布 1.4,此异常已进行了改进,以符合通用异常链接机制。即“目标异常” 在施工时提供,并通过 getTargetException()方法现在被认为是原因,并且可能是 可以通过Throwable.getCause()方法访问 前面提到的“遗留方法”。