JDK动态代理UndeclaredThrowableException异常

UndeclaredThrowableException异常背景

最近项目上出现了 JDK动态代理UndeclaredThrowableException异常,此异常之前没有接触过,那么该异常将会导致什么呢?

UndeclaredThrowableException后果:导致该抛出的直接异常信息被包装了好几层,异常看起来很费事,如下图:

产生原因

要了解其原因的前提条件是必须了解检查型异常和非检查型异常,了解的话请查看这个链接:检查型异常和非检查型异常 

为什么会产生UndeclaredThrowableException异常呢?通过查阅javadoc官方文档(链接地址:https://docs.oracle.com/javase/6/docs/api/java/lang/reflect/InvocationHandler.html),发现了该异常抛出的条件,如下图:

上述原因翻译成关键一句就是:如果抛出的异常是检查型异常,而代理类在处理异常时没有发现该类型的异常在接口中声明

这种话是什么意思呢?下面结合代码给大家讲述一下,动态代理之后会生成$Proxy0.class文件,至于怎么生成并查看这个文件,请查看这个链接:JDK动态代理文件$Proxy0.class的生成和查看, $Proxy0.class 文件内容说明了如何抛出了UndeclaredThrowableException异常,如下图:

通过JDK动态代理代码调用我们发现,method.invoke(target, args) 将会抛出 InvocationTargetException ,InvocationTargetException 异常属于检查型异常,如下图:

代码中接口声明的是非检查型异常RuntimeException,因此会抛出 UndeclaredThrowableException异常,示例demo如下所示:

// 自定义异常

public class CustomException extends RuntimeException {
    private static final long serialVersionUID = -5427543428947291283L;

    public CustomException(String message) {
        super(message);
    }
}

// 接口

public interface AccountService {
    void getAccount() throws RuntimeException;
}

// 实现类

public class AccountServiceImpl implements AccountService {
    @Override
    public void getAccount() throws RuntimeException {
        try {
            System.out.println(1/0);
        } catch (Exception e) {
            throw new CustomException("账户查询异常:" + e);
        }
    }
}

// JDK动态代理类

public class AccountProxy implements InvocationHandler {
    private Object target;

    public AccountProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(target, args);
    }
}

// 测试类

public class ProxyTest {
    public static void main(String[] args) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        AccountService accountService = new AccountServiceImpl();
        AccountService accountServiceProxy = (AccountService) Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
                accountService.getClass().getInterfaces(), new AccountProxy(accountService));

        try {
            accountServiceProxy.getAccount();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

避免抛出 UndeclaredThrowableException异常

那么针对上述代码,我们怎么样避免UndeclaredThrowableException异常的抛出呢?我们既然知道了该异常的抛出原因,那么只需要简单修改一下就可以避免此异常的抛出,即只需要将接口中抛出的异常修改为检查型异常即可,如下所示:

// 接口中的 RuntimeException 修改为了 Exception

public interface AccountService {
    void getAccount() throws Exception;
}

代理类 $Proxy0.class 如下图所示:

注意: 这里的异常类型和你自定义异常的类型无关吗,只和代理类接口中声明的异常类型有关

完美的解决方案

我们不管代理接口中声明的抛出类型是什么,我们只想看到自己抛出的直接的异常,UndeclaredThrowableException异常 和 InvocationTargetException 异常都不想看到,这样更容易定位到问题。解决方案就是在 method.invoke(target, args) 进行 InvocationTargetException 异常的捕获,代码如下:

// method.invoke(target, args) 方法调用地方增加了 InvocationTargetException 异常的捕获

public class AccountProxy implements InvocationHandler {
    private Object target;

    public AccountProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            return method.invoke(target, args);
        } catch (InvocationTargetException e) {
            throw e.getCause();
        }
    }
}

运行结果如下图所示:

总结

通过上述分析可知,当我们使用JDK动态代理的时候,需要捕获 method.invoke(target, args) 抛出的 InvocationTargetException 异常

代理类中为什么对未声明的检查型异常转换为 UndeclaredThrowableException 异常呢?答案是 java 继承原则,即子类覆盖父类方法或者实现父类方法时,抛出的异常必须在父类支持的异常列表之内。

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值