Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why runtime exception is unchecked exception?

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.

like image 853
Priyank Doshi Avatar asked Jul 18 '12 18:07

Priyank Doshi


People also ask

Is runtime exception same as unchecked exception?

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.

Why unchecked exceptions are not checked?

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.

Why checked exceptions are checked at compile time?

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.

Why do we get runtime exception?

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.


3 Answers

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.

like image 81
Jon Skeet Avatar answered Oct 18 '22 03:10

Jon Skeet


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

like image 30
ruakh Avatar answered Oct 18 '22 04:10

ruakh


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

like image 1
javajavajava Avatar answered Oct 18 '22 03:10

javajavajava