Why runtime exception is unchecked exception?

前端 未结 5 1129
庸人自扰
庸人自扰 2020-12-10 11:19

Generally if any class extends Exception , it becomes checked exception. Runtime exception also extends Exception. Then how is it unchecked e

相关标签:
5条回答
  • 2020-12-10 11:33

    Yes. Any Throwable is a checked exception, except for Error, RuntimeException, and (direct or indirect) subclasses thereof.

    However, these are checked by the compiler, not by the 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". ☺)


    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".

    0 讨论(0)
  • 2020-12-10 11:38

    It's explicitly in the specification, section 11.1.1:

    RuntimeException and all its subclasses are, collectively, the runtime exception classes.

    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.

    So yes, the compiler definitely knows about RuntimeException.

    0 讨论(0)
  • 2020-12-10 11:38

    Here is a useful link: 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)."

    0 讨论(0)
  • 2020-12-10 11:50

    As per 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 and all its subclasses are, collectively, the exception classes.

    Note that a subclass of Throwable must not be generic (§8.1.2).

    The classes Exception and Error are direct subclasses of Throwable.

    Exception is the superclass of all the exceptions from which ordinary programs may wish to recover.

    Error is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover.

    Error and all its subclasses are, collectively, the error classes.

    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.

    The class RuntimeException is a direct subclass of 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 and all its subclasses are, collectively, the run-time exception classes.

    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.

    0 讨论(0)
  • 2020-12-10 11:52

    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. 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

    0 讨论(0)
提交回复
热议问题