Home » Java programming language

Differences between Checked and UnChecked Exception in Java

Java Checked vs UnChecked Exception: Here, we are going to learn what are the differences between Checked and UnChecked Exception in Java programming language?
Submitted by Preeti Jain, on September 16, 2019

Checked vs UnChecked Exception

Here, we will see how Checked Exception differs from UnChecked Exception?

Checked Exception

  • Checked Exceptions are the exceptions which will be checked during compile time.
  • Checked Exceptions are all those exceptions which require try-catch block handling or throws keywords to specify Exception during compile time.
  • In the case of Checked Exceptions, if the compiler does not find try-catch block handling then the compiler does not throw any compilation error, but an exception will be unreported and to solve this problem we need to use either try-catch or throws.
  • We will see which predefined exceptions are Checked Exceptions (i.e. Checked Exceptions are all those exceptions which are child class of Exception class directly but we need to remember that child class must not be inherited from RuntimeException [i.e. Any child class of Exception must not be a child class of RuntimeException]).
  • All file related input/output exception comes under I/O Exception which will be Checked Exception.
  • We should go for the Checked Exception when the chances of failure of the code are higher during the operations.

We will see a few examples of Checked Exceptions, which are given below,

  1. IOException
  2. SQLException
  3. ClassNotFoundException, etc

Example:

// Java program to demonstrate the example of 
// Checked Exception

public class CheckedException {
    public static void main(String[] args) throws Exception {
        System.out.println("Exception will raise during compile-time");
    }
}

Note: To save java file with different name and run with the class name

Output

Main.java:4: error: class CheckedException is public, 
should be declared in a file named CheckedException.java
public class CheckedException {
       ^
1 error

Here, we will see how UnChecked Exception differs from Checked Exception?

UnChecked Exception

  • UnChecked Exceptions are the exceptions which will not be checked during the compile time.
  • UnChecked Exceptions are all those exceptions which do not require try-catch block handling or throws during compile time.
  • In the case of UnChecked Exceptions, if the compiler does not find try-catch block handling then the compiler will not throw a compilation error.
  • We will see which predefined exceptions are UnChecked Exceptions (i.e. UnChecked Exceptions are all those exceptions which are the child class of RuntimeException class directly i.e. we need to remember that child class must be inherited RuntimeException [i.e. Every child class of RuntimeException is UnChecked Exception]).
  • The Unchecked Exception mostly occurs due to programming errors or syntactical or logical errors and these mistakes will be done by the programmer.
  • We should go for UnChecked Exception when the chances of failure of programming mistakes are higher during syntax, logics like try to access null object, passing an illegal argument, access an element out of an array bound, etc.

We will see a few examples of UnChecked Exceptions, which are given below,

  1. ArrayIndexOutofBoundsException
  2. NullPointerException
  3. IllegalArgumentException, etc

Example:

// Java program to demonstrate the example 
// of UnChecked Exception

public class UnCheckedException {
    public static void main(String[] args) {

        int a = 10;
        int b = 0;
        int c = a / b;

        System.out.println("The value of c is :" + c);

    }
}

Output

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at UnCheckedException.main(UnCheckedException.java:9)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.