在java中扩展异常/运行时异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19857008/
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-12 20:45:38  来源:igfitidea点击:

Extending Exception/RunTimeException in java?

javaexception

提问by user755806

I have below classes.

我有以下课程。

public class ValidationException extends RuntimeException {


}

and

public class ValidationException extends Exception {


}

I am confused as to when the custom exception should extend RunTimeExceptionand when it has to extend Exception. Could you please explain me is there any disadvantage of extending RunTimeExceptiondirectly?

我对自定义异常何时应该扩展RunTimeException以及何时必须扩展感到困惑Exception。你能解释一下RunTimeException直接扩展有什么缺点吗?

Thanks!

谢谢!

采纳答案by Ankur Shanbhag

RuntimeException are unchecked while Exception are checked (calling code must handle them).

RuntimeException 未检查,而 Exception 已检查(调用代码必须处理它们)。

The custom exception should extends RuntimeExceptionif you want to make it uncheckedelse extend it with Exception.

RuntimeException如果您想取消选中自定义异常,则应扩展自定义异常否则将其扩展为Exception.

With unchecked exceptions calling code method is not required to declare in its throws clause any subclasses of RuntimeExceptionthat might be thrown during the execution of the method but not caught.

对于未经检查的异常,调用代码方法不需要在其 throws 子句中声明在RuntimeException方法执行期间可能抛出但未被捕获的任何子类。

As the calling method may not handle `RuntimeException``, one needs to be careful while throwing RuntimeException.

由于调用方法可能不处理 `RuntimeException`,所以在抛出 RuntimeException 时需要小心

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 客户端代码从它们中恢复或以任何方式处理它们。此类问题包括算术异常,例如除以零;指针异常,例如尝试通过空引用访问对象;和索引异常,例如尝试通过太大或太小的索引访问数组元素。

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).

运行时异常可能发生在程序中的任何地方,而且在一个典型的异常中,它们可能非常多。必须在每个方法声明中添加运行时异常会降低程序的清晰度。因此,编译器不需要您捕获或指定运行时异常(尽管您可以)。

Source/Further Reading:Unchecked Exceptions - The Controversy

来源/进一步阅读:未经检查的异常 - 争议

回答by Kayaman

If you extend RuntimeException, you don't need to declare it in the throws clause (i.e. it's an unchecked exception). If you extend Exception, you do (it's a checked exception).

如果您扩展RuntimeException,则不需要在 throws 子句中声明它(即它是未经检查的异常)。如果您扩展 Exception,您会这样做(这是一个已检查的异常)。

Some people argue that all exceptions should extend from RuntimeException, but if you want to force the user to handle the exception, you should extend Exceptioninstead.

有些人认为所有异常都应该从 扩展RuntimeException,但如果你想强制用户处理异常,你应该扩展Exception

回答by vels4j

Definition of RuntimeException

RuntimeException 的定义

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

RuntimeException 是那些在 Java 虚拟机正常运行期间可以抛出的异常的超类。

RuntimeException 及其子类是未经检查的异常。未经检查的异常不需要在方法或构造函数的 throws 子句中声明,如果它们可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外。

If you extend Exception, you need to catch where ever you throw your ValidationException.

如果你扩展Exception,你需要抓住你扔你的ValidationException.

回答by Batty

One case where it is common practice to throw a RuntimeException is when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

Here's the bottom line guideline: 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.

抛出 RuntimeException 的常见做法的一种情况是用户错误地调用了方法。例如,一个方法可以检查它的一个参数是否错误地为空。如果参数为 null,则该方法可能会抛出 NullPointerException,这是一个未经检查的异常。

一般来说,不要抛出 RuntimeException 或创建 RuntimeException 的子类,因为您不想为指定方法可以抛出的异常而烦恼。

这是底线准则:如果可以合理地期望客户端从异常中恢复,请将其设为已检查异常。如果客户端无法从异常中恢复,请将其设为未经检查的异常。

for more read this.

欲了解更多信息,请阅读本文。

回答by Sam

If you are in an application framework and your framework is good to handle the exception when there is an exception notification from your code. In that case you can use your custom exception class as subclass of RuntimeException.

如果您在应用程序框架中,并且当您的代码有异常通知时,您的框架可以很好地处理异常。在这种情况下,您可以使用自定义异常类作为 RuntimeException 的子类。

This will help you from NOT to write the code to handle the exception through out the exception hierarchy.

这将帮助您避免编写代码来处理整个异常层次结构中的异常。