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;
}
}
Your browser is out-of-date!

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

×