Java 异常处理 InvocationTargetException

InvocationTargetException 异常由 Method.invoke(obj, args…) 方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package me.redneno.test.reflect;

public class Reflect {
public void run(int i) throws ZeroException {
A b = new A();
a.run(i);
}
}

class A {
public void run(int i) throws ZeroException {
if (i < 0) {
throw new ZeroException("Parameter must be greater than zero!");
}
System.out.println("Param: " + 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;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package me.redneno.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("me.redneno.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("Receive uncaught exception here.");
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}

输出

1
2
3
4
5
6
7
8
9
10
11
Receive uncaught exception here.
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: me.redneno.test.reflect.ZeroException: Parameter must be greater than zero!
at me.redneno.test.reflect.A.run(Reflect.java:13)
at me.redneno.test.reflect.Reflect.run(Reflect.java:6)
... 5 more

还可以直接打印目标异常

1
2
3
4
5
6
7
...
catch (InvocationTargetException e) {
System.out.println("Receive uncaught exception here.");
Throwable t = e.getTargetException(); // Get target exception
t.printStackTrace();
}
...

输出

1
2
3
4
5
6
7
8
9
Receive uncaught exception here.
com.zzj.test.reflect.ZeroException: Parameter must be greater than zero!
at me.redneno.test.reflect.A.run(Reflect.java:13)
at me.redneno.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 me.redneno.test.reflect.Test.main(Test.java:11)

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×