正确使用RuntimeException?
什么时候应该从 RuntimeException
而不是 Exception
派生异常?
RuntimeException 不必在方法的 throws 子句中声明,这可能是好,因为它不需要不必特别列出或坏,因为显式声明方法的异常是一种很好的做法。
想法?
Possible Duplicate:
In Java, when should I create a checked exception, and when should it be a runtime exception?
When should I derive an exception from RuntimeException
instead of Exception
?
A RuntimeException
does not have to be declared in a method's throws
clause, which may be good since it doesn't have to specifically listed or bad because it is good practice to explicitly declare a method's exception.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自未经检查的异常——争议:
请注意,未经检查的异常是源自
RuntimeException
< /a> 和受检查的异常是派生自Exception
。如果客户端无法执行任何操作来从异常中恢复,为什么要抛出 RuntimeException?文章解释道:
From Unchecked Exceptions -- The Controversy:
Note that an unchecked exception is one derived from
RuntimeException
and a checked exception is one derived fromException
.Why throw a
RuntimeException
if a client cannot do anything to recover from the exception? The article explains:在企业应用程序开发中,有很多场景会使用 RuntimeException 而不是 Exception。以下是非常常见的两种此类场景:
我立即想到了这两个重要场景,但当然还有其他场景。
There are many scenarios in enterprise application development where you would use RuntimeException instead of Exception. Following are two such scenarios that are pretty common:
These are 2 significant scenarios that immediately come to my mind but there would be other scenarios of-course.