Checked exceptions in java

checked exceptions in java are also known as compile-time exceptions because these exceptions are checked by the compiler at compile time. If any method throws a checked exception, then it is programmer responsibility either handle the exception or throw the exception by using throws keyword. We can’t ignore these exceptions at compile time. The JVM checks these exceptions at the time of compilation time.

checked exception example

Lets us take an example of checked exceptions in java or compile-time exception. In this example, we are trying to read data from a file that is in our PC. So, we are creating a class CheckedExceptionExample and reading a file text with the help of FileReader. So firstly, we need to specify the address of the file which we want to read. There are two possibility that the database can contain that file or maybe not.

Checked exceptions in java
import java.io.BufferedReader;
import java.io.FileReader;

class CheckedExceptionExample 
{ 
     public static void main(String[] args) 
     { 
	FileReader fileReader  = new FileReader("D:\\abc.txt"); 
	BufferedReader bufferReader = new BufferedReader(fileReader); 		
        // We trying to print first line only so we counter should be less than 1
	for (int counter = 0; counter < 1; counter++) 
		System.out.println(bufferReader.readLine()); 		
	bufferReader.close(); 
    } 
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: Unhandled exception type FileNotFoundException Unhandled exception type IOException Unhandled exception type IOException at CheckedExceptionExample.main(CheckedExceptionExample.java:6)

Explanation of example: This exception is thrown by the compiler at compile time when if the file is not found at the specified location. When we are trying to read any file in our program then the compiler tells the programmer to handle the  FileNotFoundException and IOException exception.

We can resolve this problem we have two solutions:

  1. By using throws (Discuss it later)
  2. By use a try-catch block.

We can resolve this problem by using a try-catch block. As we discuss in the above example it is a programmer responsibility to handle Checked exceptions. So, we need to put the code in a try block and catch the exception in the catch block. FileNotFoundException is a subclass of IOException so we can use the superclass to handle both types of exceptions.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class CheckedExceptionExample 
{ 
  public static void main(String[] args)
  { 
     try
     {
        FileReader fileReader  = new FileReader("D:\\abc.txt"); 
	BufferedReader bufferReader = new BufferedReader(fileReader); 		
	 
	for (int counter = 0; counter < 1; counter++) 
	System.out.println(bufferReader.readLine()); 		
	bufferReader.close(); 
     }
     catch(IOException e)
     {
	e.printStackTrace();
     }
  }
}

Output: It will print first line of your specified file

Leave a Comment