Generally if any class extends Exception
, it becomes checked exception. Runtime exception
also extends Exception. Then how is it 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.
An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.
Because unchecked exceptions don't need to be caught, while checked exceptions do need to be handled. Thus, the compiler "forces" you to catch checked exceptions, and let your unchecked exceptions stay uncaught.
Checked exceptions are checked at compile time to ensure you are handling them, either by catching them or declaring the containing method throws the exception. At runtime, there is no distinction between checked and unchecked exceptions: they are treated identically by the JVM.
A runtime error occurs when a program is syntactically correct but contains an issue that is only detected during program execution. These issues cannot be caught at compile-time by the Java compiler and are only detected by the Java Virtual Machine (JVM) when the application is running.
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
.
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
".
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)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With