反射异常 java.lang.reflect.InvocationTargetException 处理
·
InvocationTargetException异常由Method.invoke(obj, args...)方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收。
遇到个问题,就是我们在使用反射方式调用底层代码的时候,报错java.lang.reflect.InvocationTargetException,打印scala信息
就抛出个java.lang.reflect.InvocationTargetException异常,然后没有其他的说明,使用getStackTraceString获取的时候,报错sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source),然后打印的也不是具体异常,接着查看收被调用方法内部未被捕获的异常e.getTargetException.printStackTrace()
下面是查到的一个示例:
package com.zzj.test.reflect;
public class Reflect {
public void run(int i) throws ZeroException {
B b = new B();
b.run(i);
}
}
class B {
public void run(int i) throws ZeroException {
if (i < 0) {
throw new ZeroException("参数不能小于零!");
}
System.out.println("参数:" + i);
}
}
class ZeroException extends Exception {
private static final long serialVersionUID = 1L;
private String detailMessage;
public ZeroException(String detailMessage) {
this.detailMessage = detailMessage;
}
public String getMessage() {
return detailMessage;
}
}
测试:
package com.zzj.test.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("com.zzj.test.reflect.Reflect");
Method method = clazz.getMethod("run", int.class);
method.invoke(clazz.newInstance(), -1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println("此处接收被调用方法内部未被捕获的异常");
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
输出:
此处接收被调用方法内部未被捕获的异常
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.zzj.test.reflect.Test.main(Test.java:11)
Caused by: com.zzj.test.reflect.ZeroException: 参数不能小于零!
at com.zzj.test.reflect.B.run(Reflect.java:13)
at com.zzj.test.reflect.Reflect.run(Reflect.java:6)
... 5 more
也可以直接打印目标异常:
package com.zzj.test.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("com.zzj.test.reflect.Reflect");
Method method = clazz.getMethod("run", int.class);
method.invoke(clazz.newInstance(), -1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println("此处接收被调用方法内部未被捕获的异常");
Throwable t = e.getTargetException();// 获取目标异常
t.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
输出:
此处接收被调用方法内部未被捕获的异常
com.zzj.test.reflect.ZeroException: 参数不能小于零!
at com.zzj.test.reflect.B.run(Reflect.java:13)
at com.zzj.test.reflect.Reflect.run(Reflect.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.zzj.test.reflect.Test.main(Test.java:11)
更多推荐
已为社区贡献3条内容
所有评论(0)