动态代理、反射中的Exception

来源:互联网 发布:2016淘宝对刷单的态度 编辑:程序博客网 时间:2024/06/05 11:15
//遇到invoke方法时,系统会抛出InvocationException//通过InvocationException可以获得TargetException 即原异常//如:public class ProxyHandler implements InvocationHandler {private Object obj;public ProxyHandler(Object obj) {this.obj = obj;}public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object result = null;Service s = obj.getClass().getAnnotation(Service.class);if (s != null) {// TODO do something before invoke...}try {result = method.invoke(obj, args);} catch (InvocationTargetException e1) {    // 向上抛出原始异常...throw e1.getTargetException();} finally {// TODO finally do something...}return result;}}