Checked and Unchecked Exceptions in Java

The Exception class in Java is further divided into 2 types namely Checked and Unchecked Exceptions.

What are Checked Exceptions ?

Checked exceptions are the exceptions which are known at compile time. These are the exceptions that are checked at compile time. They are also called as compile time exceptions.

Checked and unchecked exceptions need to handle using try-catch block or declare the exception using throws clause. If the exceptions are not handled properly, the program will throw compile time error. All sub classes of java.lang.Exception except sub classes of RunTimeException are checked exceptions.

Some of the Checked Exceptions in Java are:

ExceptionMeaning
NoSuchMethodExceptionException is thrown when the requested method does not exist.
NoSuchFieldExceptionException is thrown when the requested field does not exist.
ClassNotFoundExceptionException is thrown when Class not found.
CloneNotSupportedExceptionException is thrown while cloning an object that does not implement the Cloneable
interface.
InstantiationExceptionException is thrown when an attempt is made to create an object of abstract class or interface
InterruptedExceptionException is thrown when one thread has been interrupted by another thread.
IllegalAccessExceptionException is thrown when the access to a class is denied.
IOExceptionException is thrown when there is an error while performing the Input/Output operations.

Let’s understand Checked Exceptions with an Example.

Let’s try to create a myFile.txt File.

CheckedExceptionDemo.java:

Output:

If you observe the output, the program has thrown a compile time exception. This is because the IOException gets checked during compile time. Since we didn’t handle/declare the exceptions, our program gave the compile time error.

Let’s see how to resolve this exception. If you observe the output of CheckedExceptionDemo.java class, it mentions either the exception “must be caught or declared to be thrown”. So there are 2 ways of handling the exceptions either by catching the exceptions using the try-catch block or declaring an exception using the throws clause.
We will see these 2 methods one by one.

1) Handle Exception using try-catch block:

In this approach we are going to handle the exceptions using try-catch block.

CheckedExceptionDemo1.java:

Output:

If you observe the output, the file is been created successfully. In this case the program has not thrown any exception as we have handled the exception using the try-catch block.

2) Declare Exception using throws keyword:

When you don’t want to handle the exceptions using try-catch block, then declare the exception using the throws keyword in method signature. This is not the preferred approach. Approach no. 1 is the most widely used approach.

CheckedExceptionsDemo2.java:

Output:

What are Unchecked Exceptions ?

Unchecked exceptions are checked at run time. They can not be known at compile time because they occur only at run time. That’s why they are also called as RunTime Exceptions.

All the sub classes of RunTimeException and all sub classes of Error class are unchecked exceptions.

If any statement in the program throws unchecked exceptions and you are not handling them either using try-catch blocks or throws clause, then it does not give compile time error. Compilation will be successful but program may fail at run time. Therefore, to avoid premature termination of the program, you have to handle them properly.

Let’s see the unchecked exceptions with an example.

Unchecked Exception with an Example:

Output:

Let’s try to resolve this Runtime exception using try-catch block.

Output:

In the above program, there was no compilation error. But the program threw runtime exception, but we have handled it using try-catch block so the program got executed successfully.

Some of the Unchecked Exceptions are:

ExceptionMeaning
NullPointerExceptionException is thrown when invalid use of null reference is done.
ArrayIndexOutOfBoundsExceptionException is thrown when array is index is out of bounds.
ClassCastExceptionException is thrown when invalid casting of the class is done.
UnsupportedOperationExceptionException is thrown when an unsupported operation is performed.
ArithmeticExceptionException is thrown when arithmetic error occurs. For ex. divide by zero.

Related posts

Leave a Comment