InvocationTargetException异常由Method.invoke(obj, args...)方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收。
示例:
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)