How can I tell if an exception is being checked or compile-time checking?
I looked at the class RuntTimeException
and didn't check it at compile time. Why is the class not checked
at compile time and checked
at run time?
https://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html
| Throwable |
+-----------+
/ \
/ \
+-------+ +-----------+
| Error | | Exception |
+-------+ +-----------+
/ | \ / | \
\________/ \______/ \
unchecked checked
+------------------+
| RuntimeException |
+------------------+
/ | | \
\_________________/
unchecked
-
RuntimeException
class extends classException
and is still unchecked? How?. - How does the compiler decide whether to check this class at
runtime
or incompile
time ?.
source to share
RuntimeException class extends exception class and still not installed? How?.
It's a design choice. The Java language designers decided that RuntimeException and Error, and the classes that extend them, are by definition unchecked exceptions. So this is just a rule of the tongue.
How does the compiler decide whether this class should be checked at runtime or compile time?
The compiler checks the type of the thrown exception. If it is a RuntimeException or an error, the exception is not checked.
source to share
The rules are as follows:
- All exception classes that extend
RuntimeException
(includingRuntimeException
) are unchecked exceptions. - All other classes that extend
Exception
are checked exceptions - excludingRuntimeException
and anything below that. -
Error
and everything below this is also not noted.
"Checked" means that the Java compiler checks to see if your code is handling the exception by catching it, or if the method in which the exception might be thrown has a clause throws
indicating that the method can throw that type of exception.
See "Catch or Specify a Requirement" in the Oracle Java Tutorials which explain this in detail.
The answer to both questions is that it works like this because it is specified in the Java Language Specification.
source to share