Checked and Unchecked Exceptions in Java

Exception

 An unwanted unexpected event that disturbs the normal flow of the program is called an exception. PenNotFoundException
UserNotFoundException
FileNotFoundException . e
tc

It is highly recommended to handle exceptions. The main objective of exception handling is graceful (normal) termination of the program.

The exceptions which are checked by the compiler for smooth execution of the program at runtime are called checked exceptions.

FileNotFoundException
PenNotFoundException
UserNotFoundException

. etc

RuntimeException and its child classes, Error, and its child classes are unchecked and all the remaining are considered as checked exceptions.

We need to always remember that the exception is checked or unchecked compulsory it should occur at runtime only there is no chance of occurring any exception at compile time.

"throws" keyword required only checked exceptions. Usage of throws for unchecked exceptions there is no use.

Fully checked :

A checked exception is said to be fully checked if and only if all its child classes are also checked. Example:

 IOException , InterruptedException

Partially checked :

A checked exception is said to be partially checked if and only if some of its child classes are unchecked. 

 The only partially checked exceptions available in java are:

 Throwable. Exception.

The exceptions which are not checked by the compiler are called unchecked exceptions. InternetDownException
ElectricityFailureException
ArithmeticException
NullPointerException

Which of the following are checked?

RuntimeException - unchecked
Error - unchecked
IOException - fully checked
Exception - partially checked
InterruptedException - fully checked
Throwable - partially checked
ArithmeticException - unchecked
NullPointerException - - unchecked
FileNotFoundException - fully checked

Unchecked Exceptions examples

1). ArrayIndexOutOfBoundsException

It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM whenever we are trying to access array element with out of range index.

class Test { public static void main(String[] args){ int[] x=new int[10]; System.out.println(x[0]); //valid System.out.println(x[100]); //ArrayIndexOutOfBoundsException System.out.println(x[-100]); //ArrayIndexOutOfBoundsException } }

2). NullPointerException:

It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM, whenever we are trying to call any method on null.

class Test{ public static void main(String[] args){ String s=null; System.out.println(s.length()); //R.E: NullPointerException } }

3). StackOverFlowError

It is the child class of Error and hence it is unchecked. Whenever we are trying to invoke a recursive method call JVM will raise StackOverFloeError automatically.

class Test { public static void methodOne() { methodTwo(); } public static void methodTwo() { methodOne(); } public static void main(String[] args) { methodOne(); } } Output: Run time error: StackOverFloeError

4). NoClassDefFound

It is the child class of Error and hence it is unchecked. JVM will raise this error automatically whenever it is unable to find the required .class file. Example: java Test If Test.class is not available. Then we will get a NoClassDefFound error.

5). ClassCastException

It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM whenever we are trying to typecast parent object to child type.

Post/Questions related to Checked and Unchecked Exceptions in Java


Java try-catch Examples
Java Exception Interview Questions and Answers
Checked and Unchecked Exceptions in Java

In this article, we have seen  Checked and Unchecked Exceptions in Java with Examples.