如何重新抛出 IncationTargetException 的目标异常。我有一种方法,它使用反射来调用我的一个类中的 invoke() 方法。但是,如果我的代码中抛出异常,我不关心 InvocableTargetException,只需要目标异常。这是一个例子:
public static Object executeViewComponent(String name, Component c,
HttpServletRequest request) throws Exception {
try {
return c.getClass()
.getMethod(c.getMetaData().getMethod(), HttpServletRequest.class)
.invoke(c, request);
} catch (InvocationTargetException e) {
// throw the target exception here
}
}
我面临的主要问题是调用 throw e.getCause();不会抛出异常,而是抛出 Throwable。也许我的做法不正确?
catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
else {
// decide what you want to do. The cause is probably an error, or it's null.
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)