Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java know that RuntimeExceptions are unchecked?

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.

like image 413
Daniel Pereira Avatar asked Jan 14 '23 05:01

Daniel Pereira


1 Answers

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.

like image 114
assylias Avatar answered Jan 29 '23 12:01

assylias