【发布时间】:2019-03-31 13:53:10
【问题描述】:
给定一个具有静态方法的类并抛出一些异常
class Foo {
public static void doThis() throws CannotDoThisException {
//do something
}
}
我正在使用以下反射来调用 doThis 方法
public class Bar {
Class c = Class.forName("Foo");
Method m = c.getDeclaredMethod("doThis",null);
try {
m.invoke(null,null);
} catch (CannotDoThisException e) {
//Compiler says this is unreachable block.
}
}
如何捕获异常CannotDoThisException?
【问题讨论】:
-
InvokeTargetException(或类似的东西)包装被调用的方法异常。
标签: java reflection