java 检查与未检查异常

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

Checked vs Unchecked exception

javaexception-handlingchecked-exceptionsruntimeexception

提问by Laawanya

I've studied that: With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsExceptionthrown by String's charAt()method.

我已经研究过:但是,对于未经检查的异常,编译器不会强制客户端程序员捕获异常或在 throws 子句中声明它。事实上,客户端程序员甚至可能不知道可能会抛出异常。例如,StringIndexOutOfBoundsException由 String 的charAt()方法抛出。

what its mean?

这是什么意思?

according to that code there is no need to put try catch block in code, but i've seen compiler forces to put the code in try catch block.

根据该代码,无需将 try catch 块放入代码中,但我已经看到编译器强制将代码放入 try catch 块中。

I'm very confused what they are exactly?

我很困惑它们到底是什么?

回答by rodion

Unchecked exceptions are those that extend RuntimeExceptionclass. Compiler will never force you to catch such exception or force you to declare it in the method using throwskeyword. All other exception types (that do not extend RuntimeException) are checked and therefore must be declared to be thrown and/or catched.

未经检查的异常是那些扩展RuntimeException类的异常。编译器永远不会强迫您捕获此类异常或强迫您在方法 usingthrows关键字中声明它。检查所有其他异常类型(不扩展RuntimeException),因此必须声明为抛出和/或捕获。

Checked exceptions are used when you want the caller of your method (i.e the user of your API) to explicitly handle the exceptional case in your API. Checked exceptions are declared when you believe the call will be able to do something meaningful with that exceptional case, like retrying the call, rolling changes back or converting it into some user-readable error message.

当您希望您的方法的调用者(即您的 API 的用户)明确处理您的 API 中的异常情况时,使用检查异常。当您认为调用将能够对该异常情况做一些有意义的事情时,就会声明检查异常,例如重试调用、回滚更改或将其转换为一些用户可读的错误消息。

If you believe that there is nothing useful the call can do about the exception (especially when it represents a bug, or a wrong usage of your API), then the exception should be unchecked. Also, an API with too many checked exceptions can be annoying to program with (e.g. try using java reflection API=)

如果您认为调用对异常没有任何用处(尤其是当它表示错误或 API 的错误使用时),则应取消检查异常。此外,具有过多检查异常的 API 可能会令人讨厌编程(例如尝试使用 java 反射 API=)

回答by Sebastiaan van den Broek

What is your question exactly? Compilers shouldn't (and won't) enforce you to try/catch unchecked exceptions, that would go against exactly what they are.

你的问题是什么?编译器不应该(也不会)强制您尝试/捕获未经检查的异常,这将与它们的本质背道而驰。

The general idea is that checked exceptions are something you may be able to foresee but may be based on input that is out of your control and that you have to deal with. Unchecked exceptions will usually represent bugs in your program.

一般的想法是,检查异常是您可能能够预见的事情,但可能基于您无法控制且您必须处理的输入。未经检查的异常通常代表程序中的错误。

There's a number of people that think checked exceptions are a mistake in the Java platform and they only use them very sparingly or not at all. You can read more about this debate by searching google.

有很多人认为检查异常是 Java 平台中的一个错误,他们只是非常谨慎地使用它们或根本不使用它们。你可以通过搜索谷歌阅读更多关于这场辩论的信息。

回答by sgokhales

It is because,

这是因为,

  1. Unchecked Exceptions are not a result of programmer's fault. Instead they are the serious consequences where we(programmer) aren't expected to do much with it.
  2. In case of Checked Exception, it is an exception generated because of the programmer's fault & often can be resolved by programmer itself.
  1. 未经检查的异常不是程序员错误的结果。相反,它们是我们(程序员)不会对其做太多事情的严重后果。
  2. 在Checked Exception的情况下,它是由于程序员的错误而产生的异常,通常可以由程序员自己解决。

Check the following links :

检查以下链接:

Why RunTime Exceptions are unchecked ?
Checked vs Unchecked Exception ?

为什么运行时异常没有被检查?
检查与未检查异常?

回答by Paul Wintz

  • Checked Exceptions are useful for handling events that occur in the normal operation of a program. An example would be an IOExceptionthat is thrown when a file cannot be opened. These exceptions occur even if there is nothing wrong with the program. It is necessary, therefore, to tell the program how to handle the exception.
  • Unchecked exceptions are useful for identifying defects in the code. For instance, a NullPointerExceptionis thrown when a value is read on a nullobject. Thus an Unchecked Exception represents a problem that requires a manual fix by the programmer. It is reasonable for the program to crash in order to avoid erroneous behavior, so a try-catch block is not required (but might be desirable in order to provide mitigation, such as displaying an error to the user).
  • 检查异常对于处理程序正常运行中发生的事件很有用。一个例子IOException是无法打开文件时抛出的 。即使程序没有任何问题,也会发生这些异常。因此,有必要告诉程序如何处理异常。
  • 未经检查的异常对于识别代码中的缺陷很有用。例如,NullPointerException当在null对象上读取值时抛出 a 。因此,未检查异常表示需要程序员手动修复的问题。程序崩溃以避免错误行为是合理的,因此不需要 try-catch 块(但可能需要提供缓解,例如向用户显示错误)。

回答by Antony Sampath Kumar Reddy

**Checked Exceptions

**检查异常

Exceptions which are to be checked or handled or should be taken care during the time of writing the code are called as checked exceptions. For eg: 1. we have FileNotFoundException -->which will be occured when we are writing some code related to file classes. There will e defenetly posibility of non existence of file. In such case in order to handle them , we are forced to handle those exception for sure. 2. one more example is ParseException ,which will be occured when we are dealing with date functions.

在编写代码期间要检查或处理或应注意的异常称为已检查异常。例如: 1. 我们有 FileNotFoundException --> 在我们编写一些与文件类相关的代码时会发生。有可能文件不存在。在这种情况下,为了处理它们,我们肯定会被迫处理这些异常。2. 还有一个例子是 ParseException ,在我们处理日期函数时会发生。

UnChecked Exceptions

未经检查的异常

These are the exceptions that are optional to be handled during the time of coding. Its up to us whether we handle them or not. In case if we fail to handle them, There is a chance of getting runtime errors during the exceution. For eg: We have something called NullPointerException,ArithemeticException,NosSuchElementFoundException and so on. These are like optional things we dont even have to handle them. More over even jvm or compiler will not recommend us to handle them.**

这些是在编码期间可选择处理的异常。我们是否处理它们取决于我们。如果我们无法处理它们,则有可能在执行过程中出现运行时错误。例如:我们有一个叫做 NullPointerException、ArithemeticException、NosSuchElementFoundException 的异常等等。这些就像可选的东西,我们甚至不必处理它们。甚至 jvm 或编译器也不建议我们处理它们。**

回答by Vidur Agarwal

All Exceptions are part of run time and not compile time. There are two kinds of exceptions checked exceptions and unchecked exceptions. Examples of checked exceptions are IO Exceptions, ClassNotFound Exception and examples of unchecked exceptions are runtime exceptions. In the case of checked exceptions, error or warning message comes at compile time so that the user will take care of it at runtime by using throws keyword, which is used to throw exceptions to the default catch mechanism system. But in case of unchecked exceptions warning is not there at compile time.

所有异常都是运行时的一部分,而不是编译时。异常有受检异常和非受检异常两种。已检查异常的示例是 IO 异常、ClassNotFound 异常,未检查异常的示例是运行时异常。在检查异常的情况下,错误或警告消息会在编译时出现,以便用户在运行时使用 throws 关键字来处理它,该关键字用于向默认捕获机制系统抛出异常。但是如果出现未经检查的异常警告,则在编译时不存在。