简体   繁体   English

为什么运行时异常是未经检查的异常?

[英]Why runtime exception is unchecked exception?

Generally if any class extends Exception , it becomes checked exception.通常,如果任何类扩展了Exception ,它就会成为已检查的异常。 Runtime exception also extends Exception. Runtime exception也扩展了异常。 Then how is it unchecked exception ?那么它是如何unchecked exception呢?

Is it like they have a custom check in compiler for this special case?对于这种特殊情况,他们是否对编译器进行了自定义检查?

EDIT : I have proper idea about checked v/s unchecked exception and their pros & cos etc. I don't accept differences between them in answer.编辑:我对已检查的 v/s 未检查的异常及其优点和 cos 等有正确的了解。我不接受它们之间的差异作为答案。

It's explicitly in the specification, section 11.1.1 :它在规范中明确指出, 第 11.1.1 节

RuntimeException and all its subclasses are, collectively, the runtime exception classes . RuntimeException及其所有子类统称为运行时异常类

The unchecked exception classes are the runtime exception classes and the error classes.未经检查的异常类是运行时异常类和错误类。

The checked exception classes are all exception classes other than the unchecked exception classes.检查异常类是除未检查异常类之外的所有异常类。 That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.也就是说,被检查的异常类除了RuntimeException及其子类和Error及其子类之外,都是Throwable的子类。

So yes, the compiler definitely knows about RuntimeException .所以是的,编译器肯定知道RuntimeException

Yes.是的。 Any Throwable is a checked exception, except for Error , RuntimeException , and (direct or indirect) subclasses thereof.任何Throwable都是已检查的异常,但ErrorRuntimeException及其(直接或间接)子类除外。

However, these are checked by the compiler , not by the JVM;但是,这些是由编译器检查的,而不是 JVM; checked exceptions are a compile-time feature, not a run-time feature.已检查异常是编译时功能,而不是运行时功能。 ( Update: And I now see that you've edited your question to specify "compiler" rather than "JVM". ☺) 更新:我现在看到您已经编辑了您的问题以指定“编译器”而不是“JVM”。☺)


To elaborate a bit further .再详细说明一下。 . . . . it's not as though there were any sort of "checked-exception" interface.好像没有任何类型的“检查异常”接口。 The logic is simply hard-coded: "any exception class is a checked exception unless it's a subtype of RuntimeException or Error ".逻辑只是硬编码:“任何异常类都是已检查异常,除非它是RuntimeExceptionError的子类型”。

Here is a useful link: http://www.javapractices.com/topic/TopicAction.do?Id=129这是一个有用的链接: http : //www.javapractices.com/topic/TopicAction.do?Id=129

It explains the difference between unchecked and checked and gives some examples.它解释了未检查和已检查之间的区别并给出了一些示例。

"It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked)." “这有点令人困惑,但请注意,RuntimeException(未检查)本身就是 Exception(已检查)的子类。”

As per 11.1.1.按照11.1.1。 The Kinds of Exceptions异常的种类

An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.异常由 Throwable 类(Object 的直接子类)或其子类之一的实例表示。

Throwable and all its subclasses are, collectively, the exception classes. Throwable 及其所有子类统称为异常类。

Note that a subclass of Throwable must not be generic (§8.1.2).请注意,Throwable 的子类不能是通用的(第 8.1.2 节)。

The classes Exception and Error are direct subclasses of Throwable. Exception 和 Error 类是 Throwable 的直接子类。

Exception is the superclass of all the exceptions from which ordinary programs may wish to recover. Exception 是普通程序可能希望从中恢复的所有异常的超类。

Error is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover. Error 是所有异常的超类,普通程序通常不会从中恢复。

Error and all its subclasses are, collectively, the error classes. Error 及其所有子类统称为错误类。

The class Error is a separate subclass of Throwable, distinct from Exception in the class hierarchy, to allow programs to use the idiom "} catch (Exception e) {" (§11.2.3) to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible. Error 类是 Throwable 的一个单独子类,在类层次结构中与 Exception 不同,以允许程序使用习语“} catch (Exception e) {”(第 11.2.3 节)来捕获所有可以从中恢复的异常没有捕获通常无法恢复的错误。

The class RuntimeException is a direct subclass of Exception. RuntimeException 类是 Exception 的直接子类。 RuntimeException is the superclass of all the exceptions which may be thrown for many reasons during expression evaluation, but from which recovery may still be possible. RuntimeException 是所有异常的超类,在表达式计算过程中可能由于多种原因抛出这些异常,但仍然可以从中恢复。

RuntimeException and all its subclasses are, collectively, the run-time exception classes. RuntimeException 及其所有子类统称为运行时异常类。

The unchecked exception classes are the run-time exception classes and the error classes.未经检查的异常类是运行时异常类和错误类。

The checked exception classes are all exception classes other than the unchecked exception classes.已检查异常类是除未检查异常类之外的所有异常类。 That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.也就是说,被检查的异常类除了RuntimeException及其子类和Error及其子类之外,都是Throwable的子类。

Run-time exception is called unchecked exception since it's not checked during compile time.运行时异常称为未检查异常,因为它在编译时未检查。 Everything under throwable except ERROR and RuntimeException are checked exception.除了ERRORRuntimeException之外, throwable 下的所有内容都是检查异常。 Adding Runtime exception in program will decrease the clarity of program.在程序中添加运行时异常会降低程序的清晰度。

class Divide {
    public static void main(String [] args){
        int a = 10;
        int b = 0;
        int c = a/b; // This will throw run time exception due to unexpected value of b.
    }
}

Please read this link The Java™ Tutorials - Unchecked Exceptions — The Controversy请阅读此链接The Java™ Tutorials - Unchecked Exceptions - The Controversy

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

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