Checked Exceptions in Java

These are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exceptions, there are two types: fully checked and partially checked exceptions. A fully checked exception is a checked exception where all its child classes are also checked, like IOException, and InterruptedException. A partially checked exception is a checked exception where some of its child classes are unchecked, like an Exception.

For example, consider the following Java program that opens the file at location “C:\test\a.txt” and prints the first three lines of it. The program doesn’t compile, because the function main() uses FileReader(), and FileReader() throws a checked exception FileNotFoundException. It also uses readLine() and close() methods, and these methods also throw checked exception IOException

Example:

Java




// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException occurred
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Reading file from path in local directory
        FileReader file = new FileReader("C:\\test\\a.txt");
 
        // Creating object as one of ways of taking input
        BufferedReader fileInput = new BufferedReader(file);
 
        // Printing first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
 
        // Closing file connections
        // using close() method
        fileInput.close();
    }
}


Output: 

To fix the above program, we either need to specify a list of exceptions using throws, or we need to use a try-catch block. We have used throws in the below program. Since FileNotFoundException is a subclass of IOException, we can just specify IOException in the throws list and make the above program compiler-error-free.

Example:

Java




// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException does not occur
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Creating a file and reading from local repository
        FileReader file = new FileReader("C:\\test\\a.txt");
 
        // Reading content inside a file
        BufferedReader fileInput = new BufferedReader(file);
 
        // Printing first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
 
        // Closing all file connections
        // using close() method
        // Good practice to avoid any memory leakage
        fileInput.close();
    }
}


Output: 

First three lines of file "C:\test\a.txt"

Checked vs Unchecked Exceptions in Java

In Java, Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions. 

In Java, there are two types of exceptions:

  1. Checked exceptions
  2. Unchecked exceptions

Similar Reads

Checked Exceptions in Java

These are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exceptions, there are two types: fully checked and partially checked exceptions. A fully checked exception is a checked exception where all its child classes are also checked, like IOException, and InterruptedException. A partially checked exception is a checked exception where some of its child classes are unchecked, like an Exception....

Unchecked Exceptions in Java

...