How does Java know that RuntimeExceptions are unchecked?
exception, java
Solution
Because it is defined that way in the specification of the language # 11.1.1:
- 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 it is not part of the code of the various exception classes, it is only a convention defined by the language, that compilers must implement to be compliant.
Problem
How does the Java compiler know that `java.lang.RuntimeException` and its subclasses are unchecked, since they inherit from `java.lang.Exception`, which is a checked exception? I looked into the code and doesn't seem to be anything inside the class that tells it to the compiler.