Difference between Checked and Unchecked exception in Java

Share

In this article, we’ll be discussing the meaning of exceptions, their type, and the difference between checked and unchecked exceptions.

An exception can be defined as any event that causes an interruption in executing a program and disrupts its normal flow. ( What happens when an exception is occurred ? )

It can be handled using an exception handler with the help Try, Catch, finally, and resources components. An effective exception handler can prevent a program to get terminated forcefully.

 

In Java, Runtime exception and error are together known as Unchecked exception.

An exception can be classified into two types:-

  1. Checked Exception
  2. Unchecked Exception

Further, “Throwable” is the super class of Error and Exception class.

The basic difference between these two types of exceptions is,  Checked exceptions are identified at compile time, whereas Unchecked exceptions are those which are not identified at compile time by the compiler.

 

Difference between Checked and Unchecked Exception in Java

CHECKED EXCEPTIONUNCHECKED EXCEPTION
An exception that triggers at compile timeAn exception that triggers at runtime
A compiler can anticipate these exceptions and can be handled effectively and efficientlyA compiler can't anticipate these exceptions. Its programmer's responsibility to deal with these exceptions
All exceptions under Exception class are referred checked exception, except RuntimeException and its subclasses,RuntimeException and Errors classes are together referred unchecked exception and not a part of Exception class
It identifies and encounters compile time exception, if it is not handledIt doesn't identify and encounter compile time exception and programmer is responsible for it
For example, FileNotFoundExceptionFor example, NullPointerException and IOError

Checked exception

  1. This exception arises due to our program or application.
  2. This exception is certain to the compiler and that’s why it is predictable and can be handled efficiently.
  3. It is recoverable.
  4. For Example, in a program, if you try to read a file that does not exist, then the compiler would trigger an error on to the screen.
  5. These are compile-time errors.
  6. The thrown exception can be caught by catch statements.

 

Java Program to demonstrate checked exception ?

    public static void main(String[] args) {

        File file = new File("c/programmerbay");
        FileInputStream stream = new FileInputStream(file);  // Compile time error, need to enclose this line in try catch block

}

Unchecked Exception

An unchecked exception can be classified in two types :

  • Runtime Exception

  1. This exception is also caused by our program, it occurs due to logical errors or irrational use of API.
  2. The exception cannot be predicted by the compiler.
  3. It is irrecoverable.
  4. For example, suppose, you’re trying to read a file by passing the name of the file but somehow, due to logical error, a null gets passed to the constructor, the constructor would throw a respective runtime Exception.
  5. These are run-time errors.
  6. The thrown exception can be caught and handled only if the programmer aware of the problem, otherwise it is difficult to catch. However, it is better to avoid this situation.

Java Program to demonstrate unchecked exception ?

  public static void main(String[] args) {

        int x = 5;
        int y = 0 ;
        int result = x/y;  // Exception would be occurred after executing the program at this line, ArrayIndexOutOfBoundsException
}
  • Errors

  1. This exception is not arisen by our application, instead of this, it occurs because of the lack of system resources.
  2. This cannot be anticipated by the compiler and therefore it is difficult to handle.
  3. It is irrecoverable
  4. For Example, your system’s heap memory may be got full and it runs out of the memory. resulting your program would not be executed, here, your application isn’t accounted for this, a programmer can’t do anything in this situation.
  5. These are also identified at runtime.
  6. The thrown exception cannot be handled by catch statements as these exceptions are external to your application, most of the time applications end up with termination.

 

This post was last modified on April 8, 2021

Sandeep Verma

Published by
Sandeep Verma
Tags: checked and unchecked exception in java Difference Difference between Checked and Unchecked exception errors exception handling java runtime errors unchecked