Java 正确使用 RuntimeException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4233793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:50:39  来源:igfitidea点击:

Proper use of RuntimeException?

javaexceptionruntimeexception

提问by Tony the Pony

Possible Duplicate:
In Java, when should I create a checked exception, and when should it be a runtime exception?

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

When should I derive an exception from RuntimeExceptioninstead of Exception?

我什么时候应该从而RuntimeException不是派生异常Exception

A RuntimeExceptiondoes not have to be declared in a method's throwsclause, which may be goodsince it doesn't have to specifically listed or badbecause it is good practice to explicitly declare a method's exception.

ARuntimeException不必在方法的throws子句中声明,这可能是好的,因为它不必特别列出或不好,因为显式声明方法的异常是一种很好的做法。

Thoughts?

想法?

采纳答案by Derek Mahar

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 RuntimeExceptionand a checked exception is one derived from Exception.

请注意,未经检查的异常是从 派生的RuntimeException,检查的异常是从 派生的Exception

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

RuntimeException如果客户端无法从异常中恢复,为什么要抛出 a ?文章解释说:

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.

运行时异常表示由编程问题导致的问题,因此,不能合理地期望 API 客户端代码从它们中恢复或以任何方式处理它们。此类问题包括算术异常,例如除以零;指针异常,例如尝试通过空引用访问对象;和索引异常,例如尝试通过太大或太小的索引访问数组元素。

回答by Rutesh Makhijani

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:

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

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

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

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