用运行时异常替换检查的异常?

编程入门 行业动态 更新时间:2024-10-26 08:23:10
本文介绍了用运行时异常替换检查的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 鉴于我基本上想要消除检查的异常使用并将其转换为运行时异常,我通常会这样做:

try { file.read(); } catch(IOException e){ throw new RuntimeException(e); }

这样做有几个缺点,但最令我激怒的一个我的运行时异常会包含一个嵌套的堆栈跟踪。基本上我想重新抛出IOException作为RuntimeException(或IORuntimeException)与原始消息和stacktrace,所以我可以避免无用的嵌套堆栈跟踪。在中间的某个地方重新抛出异常的事实似乎对我来说只是无用的噪音。

这可能吗?有没有图书馆这样做?

解决方案

项目龙目岛允许您完全禁用检查的例外。

Given that I basically want to eliminate checked exception usage and transform them to runtime exceptions, I would normally be doing something like this:

try { file.read(); } catch (IOException e){ throw new RuntimeException(e); }

There are several disadvantages to doing this, but the one that irritates me the most is that my runtime exception would contain a nested stacktrace. Basically I would like to re-throw the "IOException" as a RuntimeException (or "IORuntimeException") with the original message and stacktrace, so I can avoid the useless nested stacktrace. The "fact" that I have re-thrown the exception somewhere in the middle seems like just useless noise to me.

Is this possible ? Is there any library that does this ?

解决方案

Project Lombok allows you to disable checked exceptions altogether.

更多推荐

用运行时异常替换检查的异常?

本文发布于:2023-11-30 21:37:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!