Java转换异常(不是类转换异常) [英] Java casting an exception (not class cast exception)

查看:171
本文介绍了Java转换异常(不是类转换异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当异常被捕获在java中有一个用例来将异常转换为新的类型?或者是标准

When an exception is caught in java is there a use case for casting the exception to a new type? Or is the standard

    throw new DiffException(e)

这是唯一的方法。对于ClassCastExceptions这个显然不是我正在寻找的搜索结果

The only way to do it. I apologize if I'm overlooking something but the only search results I get are for "ClassCastExceptions" Which is obviously not what I'm looking for

推荐答案

我相信您的意思是例外环绕
没有其他方法可以做到 - 您使用一个构造函数创建一个 Exception 的新实例,该构造函数接受另一个异常作为原因。由于 java.lang.Exception的1参数构造函数。典型的自定义异常类型实现(像你的 DiffException )也声明这样的1-arg构造函数。

I believe you meant 'exception wrapping'. There's no other way to do it - you create a new instance of Exception using a constructor which takes another exception as cause. This works thanks to 1-arg constructor of java.lang.Exception. The typical implementation of custom exception type (like your DiffException) declares such 1-arg constructor too.

这篇关于Java转换异常(不是类转换异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆