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)
java.lang.reflec.InvocationTargetException
捕获这个异常,然后通过getTargetException() 方法获得异常,这个可以获得原方法抛出的异常。
然后可以通过类型判断,判断到底抛出的是原异常的哪一个异常。
Class>[]getExceptionTypes()
返回 Class 对象的数组,这些对象描述了声明将此 Method 对象表示的底层方法抛出的异常类型。Method对象通过该方法可以的到方法声明时异常抛出的类型,可以手工判断进行抛出,但是有一点给注意,如果在运行时抛出异常 如 RuntimeException类型的错误,如果再声明里没有写明,应该领去处理。