Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Unchecked exception or runtime exception

This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.

like image 947
giri Avatar asked Apr 23 '10 15:04

giri


People also ask

What is the difference between unchecked and checked exceptions?

Checked Exceptions are checked at runtime of the program, while Unchecked Exceptions are checked at the compile time of the program. Checked Exceptions and Unchecked Exceptions both can be created manually. Checked Exceptions and Unchecked Exceptions both can be handled using try, catch and finally.

Why runtime exceptions are not checked?

Because the Java programming language does not require methods to catch or to specify unchecked exceptions ( RuntimeException , Error , and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException .

Which of the following is an example of runtime unchecked exception?

Java does not verify unchecked exceptions at compile-time. And although the above code does not have any errors during compile-time, it will throw ArithmeticException at runtime. Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.


3 Answers

As stated by their name, unchecked exceptions are not checked at compile-time which means that the compiler doesn't require methods to catch or to specify (with a throws) them. Classes belonging to this category are detailed in the section 11.2 Compile-Time Checking of Exceptions of the JLS:

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See §11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.

The following picture illustrates the Exception hierarchy:

alt text

The class Error and its subclasses are exceptions from which ordinary programs are not ordinarily expected to recover and, as explained in 11.5 The Exception Hierarchy:

The class Error is a separate subclass of Throwable, distinct from Exception in the class hierarchy, to allow programs to use the idiom:

} catch (Exception e) { 

to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible.

To summarize, RuntimeException are a subset of unchecked exceptions for exceptions from which recovery is possible (but unchecked exception is not a synonym of RuntimeException as many are answering here).

like image 155
Pascal Thivent Avatar answered Sep 29 '22 04:09

Pascal Thivent


Note: a RuntimeException IS an unchecked exception

An unchecked exception would be one that is known to be possible at a point in the execution but is not caught, for example a NullPointerException is always a possibility if you don't check for them and will cause your program to terminate. You could check for it by wrapping code in try-catch, but this is not enforced (unlike a checked exception that will enforce that the exception is handled in some way).

An error is something that can occur at any point during execution and can't really be caught because it is not eplicitly caused by a particular method call etc. For example an OutOfMemoryError or a StackOverflowError. Both of these could occur at any time and will cause your application to terminate. Catching these errors make no sense as they indicate that something has happened that you won't be able to recover from.

like image 27
DaveJohnston Avatar answered Sep 29 '22 04:09

DaveJohnston


The JavaDocs sum these up pretty well.

java.lang.RuntimeException:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

java.lang.Error:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

Note that "unchecked exception" is merely a synonym for a RuntimeException.

like image 25
matt b Avatar answered Sep 29 '22 04:09

matt b