This is one of the hottest Java Interview Questions for entry level and mid level software developers in Java. The question is mostly asked as “Explain Exception Handling in Java” but the wordings may change.
Defining an Exception
An exception indicates a problem that a reasonable application may wish to recover from.
On a more formal note, “When a program violates the semantic constraints of the Java Programming Language, the JVM signals error to the program as an Exception. Many programming languages abruptly terminates programs in such scenarios but Java has a cleaner way of handling these situations.”
For e.g: Attempting to index an element outside the boundary of an array.
When an exception occurs, a non local transfer of the program control happens from the place where the exception occurred to the place specified by the programmer (catch block). The exception carries with itself some information about the exception condition (cause and an optional message).
It propagates till it reaches a user defined handler (a catch block).
In case there is no user defined exception handlers for a given exception situation, the exception may get handled by any of the defined UncaughtExceptionHandler hierarchy or the default UncaughtExceptionHandler.
Exceptions & Errors
java.lang.Throwable is the super class of all errors and exceptions. The two sub classes of Throwable are java.lang.Error and java.lang.Exception. Errors indicate serious problems that a reasonable application should not try to catch. Few of the important Error classes are StackOverflowError, OutOfMemoryError, NoClassDefFoundError.
Checked & Unchecked Exception
A logical classification of all the exceptions and errors can also be done in two classes, unchecked exceptions and checked exceptions.
Unchecked Exceptions include the following:
- RuntimeException and its sub classes
- Error and its sub classes
Checked Exceptions include all exceptions and their sub classes other than RuntimeException
Why do we need Checked Exception?
Checked Exceptions are eligible to be checked for existence of a handler at compile time. This precisely means that the programmer is required to supply a handler for an exception whose code will execute and attempt to recover from the exception situation, and if he/she fails to supply one, the compiler will immediately know about it and throw an error at the compile time.
It is designed so to reduce the number of exceptions which are not properly handled.
Why do we need Unchecked Exception?
Unchecked Exceptions are exempted from compile time check, this is the reason we need them. The two types of Unchecked Exception
- Error – Errors can absolutely occur at any place and it is very difficult to recover from them, hence it would be complicated and cluttered to write a handler everywhere. So, they are exempted from compile time check.
- Runtime Exception – It is exempted from a compile time check because the information available to a Java compiler, and the level of analysis a compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer.