Dynamic Proxy的异常处理
程序员文章站
2022-07-14 14:17:57
...
现在越来越感觉Dynamic Proxy是个好东西,不过发现异常处理还是得注意下,因为调用实际的代码返回的异常都会被包一次成为InvocationTargetException,所以如果你想让原本的异常继续往上抛,那么必须捕获异常并且得到targetException然后再抛出去:
try {
result = targetMethod.invoke(targetObject, args);
} catch (IllegalAccessException e) {
throw new RuntimeException("IllegalAccessException", e);
} catch (IllegalArgumentException e) {
throw new RuntimeException("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}