简体   繁体   English

已检查与未检查的异常

[英]Checked vs Unchecked exception

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.我研究过:但是,对于未经检查的异常,编译器不会强制客户端程序员捕获异常或在 throws 子句中声明它。 In fact, client programmers may not even know that the exception could be thrown.事实上,客户端程序员甚至可能不知道异常可能会被抛出。 eg, StringIndexOutOfBoundsException thrown by String's charAt() method.例如,String 的charAt()方法抛出的StringIndexOutOfBoundsException

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?我很困惑他们到底是什么?

Unchecked exceptions are those that extend RuntimeException class.未经检查的异常是那些扩展RuntimeException类的异常。 Compiler will never force you to catch such exception or force you to declare it in the method using throws keyword.编译器永远不会强迫您捕获此类异常或强迫您在方法中使用throws关键字声明它。 All other exception types (that do not extend RuntimeException ) are checked and therefore must be declared to be thrown and/or catched.检查所有其他异常类型(不扩展RuntimeException ),因此必须声明为被抛出和/或捕获。

Checked exceptions are used when you want the caller of your method (ie the user of your API) to explicitly handle the exceptional case in your API.当您希望方法的调用者(即 API 的用户)显式处理 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.当您认为调用能够对这种异常情况执行一些有意义的事情时,就会声明检查的异常,例如重试调用、回滚更改或将其转换为一些用户可读的错误消息。

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.如果您认为该调用对异常没有任何用处(尤其是当它表示错误或 API 的错误使用时),则应取消选中该异常。 Also, an API with too many checked exceptions can be annoying to program with (eg try using java reflection API=)此外,带有太多已检查异常的 API 可能会令人讨厌(例如,尝试使用 java 反射 API=)

  • Checked Exceptions are useful for handling events that occur in the normal operation of a program. Checked Exceptions 对于处理程序正常操作中发生的事件很有用。 An example would be an IOException that is thrown when a file cannot be opened.一个示例是在无法打开文件时引发的IOException 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 NullPointerException is thrown when a value is read on a null object.例如,在null对象上读取值时会引发NullPointerException 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).为了避免错误行为,程序崩溃是合理的,因此不需要 try-catch 块(但可能需要提供缓解措施,例如向用户显示错误)。

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.有很多人认为检查异常在 Java 平台中是一个错误,他们只是非常谨慎地使用它们或根本不使用它们。 You can read more about this debate by searching google.你可以通过搜索谷歌阅读更多关于这场辩论的信息。

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.对于Checked Exception,是由于程序员的错误而产生的异常,通常可以由程序员自己解决。

Check the following links :检查以下链接:

Why RunTime Exceptions are unchecked ? 为什么未选中运行时异常?
Checked vs Unchecked Exception ? 已检查与未检查的异常?

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.已检查异常的示例是 IO 异常、ClassNotFound 异常,未检查异常的示例是运行时异常。 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.在检查异常的情况下,错误或警告消息在编译时出现,以便用户在运行时使用 throws 关键字处理它,该关键字用于将异常抛出到默认的 catch 机制系统。 But in case of unchecked exceptions warning is not there at compile time.但是如果出现未经检查的异常,编译时不会出现警告。

**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.例如: 1. 我们有 FileNotFoundException --> 当我们编写一些与文件类相关的代码时会发生这种情况。 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. 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.例如:我们有一个叫做 NullPointerException、ArithemeticException、NosSuchElementFoundException 等的东西。 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.**甚至 jvm 或编译器也不建议我们处理它们。**

in simple words,简单来说,

  • checked exceptions are those which can be and should be handled by your code(therefore compiler forces you to handle them)检查异常是那些可以并且应该由您的代码处理的异常(因此编译器会强制您处理它们)
  • unchecked exceptions are those which lie beyond programmer's control(therefore compiler doesn't forces you to handle them)未经检查的异常是超出程序员控制范围的异常(因此编译器不会强迫您处理它们)

use the same rule even while creating your custom exceptions.即使在创建自定义例外时也使用相同的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM