正确使用RuntimeException?

发布于 2024-10-03 16:57:55 字数 471 浏览 2 评论 0原文

可能的重复:
在Java中,什么时候应该创建检查异常,什么时候应该创建运行时异常?

什么时候应该从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-10-10 16:57:55

来自未经检查的异常——争议

如果可以合理地预期客户
要从异常中恢复,请使其
一个已检查的异常。如果一个客户
无法采取任何措施来恢复
异常,使其成为未经检查的
异常。

请注意,未经检查的异常是源自 RuntimeException< /a> 和受检查的异常是派生自 Exception

如果客户端无法执行任何操作来从异常中恢复,为什么要抛出 RuntimeException?文章解释道:

运行时异常代表问题
这是编程的结果
问题,因此,API 客户端
不能合理地期望代码
从中恢复或处理它们
反正。此类问题包括
算术异常,例如
除以零;指针异常,
例如尝试访问一个对象
通过空引用;和索引
例外情况,例如尝试
通过访问数组元素
索引太大或太小。

From Unchecked Exceptions -- The Controversy:

If a client can reasonably be expected
to recover from an exception, make it
a checked exception. If a client
cannot do anything to recover from the
exception, make it an unchecked
exception.

Note that an unchecked exception is one derived from RuntimeException and a checked exception is one derived from Exception.

Why throw a RuntimeException if a client cannot do anything to recover from the exception? The article explains:

Runtime exceptions represent problems
that are the result of a programming
problem, and as such, the API client
code cannot reasonably be expected to
recover from them or to handle them in
any way. Such problems include
arithmetic exceptions, such as
dividing by zero; pointer exceptions,
such as trying to access an object
through a null reference; and indexing
exceptions, such as attempting to
access an array element through an
index that is too large or too small.

没有伤那来痛 2024-10-10 16:57:55

在企业应用程序开发中,有很多场景会使用 RuntimeException 而不是 Exception。以下是非常常见的两种此类场景:

  • 在将异常处理作为一个方面实现(分离关注点设计原则)时,在大多数现代框架中,您将声明性地处理异常并关联特定的异常处理块,而不是对其进行硬编码。一个很好的例子是 Spring 中的 JDBC 模板,它将所有 SQL 异常转换为 RuntimeException,因此开发人员在编写数据访问逻辑时无需编写 try catch 块。您可以以声明方式定义异常处理程序,该处理程序可以在开发环境中提供不同的行为。以及生产中的不同行为。 Struts 1.x Action 类中也有类似的实现,其中执行方法被声明为抛出异常,并且在 struts-config 中映射了单独的 ExceptionHandler 来处理特定的异常。虽然这不是 RuntimeException 的示例,但设计原理是相同的,将正常执行和异常处理的关注点分开。
  • RuntimeException 的另一个用途是在 EJB 和其他事务管理器中,其中事务由容器进行控制器。按照惯例,在此类容器中,如果您从代码中抛出 RuntimeException,则事务将回滚 - 如果您抛出异常,则不会发生同样的情况。

我立即想到了这两个重要场景,但当然还有其他场景。

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:

  • While implementing Exception handling as an aspect (separating the concern design principle), in most modern day frameworks you would declarative handle exceptions and associate specific exception handling blocks rather than hardcoding the same. One good example of this is JDBC template in Spring that converts all the SQL exceptions to RuntimeException so developer doesnot write try catch blocks while writting data access logic. you can define exception handler declaratively that can provide different behavior in dev env. and different behavior in production. Similar implementation is there in Struts 1.x Action class also, where the execute method is declared to throw Exception and there is separate ExceptionHandler mapped in struts-config for handling specific exceptions. Though this is not example of RuntimeException but the design principle is same to separate the concern of normal execution and exception handling.
  • Another use of RuntimeException is in EJB and other Transaction Managers where in the transactions are controller by container. In such containers by convention if you throw RuntimeException from within your code the transaction would rollback - the same would not happen if you throw an Exception.

These are 2 significant scenarios that immediately come to my mind but there would be other scenarios of-course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文