Vin*_*ano 8 java exception-handling invocationtargetexception
如何重新抛出InvocationTargetException的目标异常.我有一个方法,它使用反射来调用我的一个类中的invoke()方法.但是,如果在我的代码中抛出异常,我不关心InvocationTargetException并且只想要目标异常.这是一个例子:
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.也许我接近这个错误?
JB *_*zet 16
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.
    }
}
| 归档时间: | 
 | 
| 查看次数: | 11328 次 | 
| 最近记录: |