Checked Exception

Checked exceptions are also called “compile Time exceptions”

Compiler checks those exceptions during the compilation to see whether they are handled or not.

If they are not handled, compiler will force us to handle and until we handle it, we get compile time error and 
we can’t proceed further.


Since compiler “checks” for these exceptions to make sure that they are handled, it is called Checked exception.

So, whenever a method throws checked exception then caller should handle it either using try/catch
(OR)
It can use throws to send this exception handling to its caller.

Let’s see an example for checked exception with below program


In this program, we are going to read a file called “test.txt” and display its content on console.

We will see few places in this program where there is a possibility of checked exception and same is mentioned in the comments

  1. import java.io.*;
  2. class FileIOExample {  
  3.    public static void main(String args[])
  4.    {
  5.     FileInputStream fis = new FileInputStream("C:/test.txt");
  6.     /*This line FileInputStream(File filename)
  7.      * throws FileNotFoundException which is a checked
  8.      * exception
  9.          */
  10.    
  11.     int charRead=0;
  12.  
  13.     /* read() method of FileInputStream class also throws
  14.      * a checked exception: IOException
  15.          */
  16.     while(( charRead = fis.read() ) != -1)
  17.     {
  18.         System.out.print((char)charRead);
  19.     }
  20.  
  21.     /*The close() methodd also throws a checked exception: IOException*/
  22.     fis.close();    
  23.    }
  24. }
import java.io.*;
class FileIOExample {  
   public static void main(String args[]) 
   {
	FileInputStream fis = new FileInputStream("C:/test.txt");
	/*This line FileInputStream(File filename)
	 * throws FileNotFoundException which is a checked
	 * exception
         */
    
	int charRead=0; 

	/* read() method of FileInputStream class also throws 
	 * a checked exception: IOException
         */
	while(( charRead = fis.read() ) != -1) 
	{ 
		System.out.print((char)charRead); 
	} 

	/*The close() methodd also throws a checked exception: IOException*/
	fis.close(); 	
   }
}


We could see that, program throws compile time error but why?

As we have already understood that, checked exceptions has to be handled otherwise compiler will force us to handle it.

Yes, we have no option but to handle these exceptions to run this program successfully

How to handle these exceptions?

We can handle it either by using try/catch block or by using throws

Handling using throws


This way of handling exception is actually not always a good choice as we are passing the responsibility of exception handling to caller of a method.

If caller is handling it then its better, Otherwise it is very bad option as exception stack trace will be displayed to user instead of meaningful message.

  1. import java.io.*;
  2. class FileIOExample {  
  3.    public static void main(String args[]) throws IOException
  4.    {
  5.     FileInputStream fis = new FileInputStream("C:/test.txt");
  6.     /*This line FileInputStream(File filename)
  7.      * throws FileNotFoundException which is a checked
  8.      * exception
  9.          */
  10.    
  11.     int charRead=0;
  12.  
  13.     /* read() method of FileInputStream class also throws
  14.      * a checked exception: IOException
  15.          */
  16.     while(( charRead = fis.read() ) != -1)
  17.     {
  18.         System.out.print((char)charRead);
  19.     }
  20.  
  21.     /*The close() methodd also throws a checked exception: IOException*/
  22.     fis.close();    
  23.    }
  24. }
import java.io.*;
class FileIOExample {  
   public static void main(String args[]) throws IOException 
   {
	FileInputStream fis = new FileInputStream("C:/test.txt");
	/*This line FileInputStream(File filename)
	 * throws FileNotFoundException which is a checked
	 * exception
         */
    
	int charRead=0; 

	/* read() method of FileInputStream class also throws 
	 * a checked exception: IOException
         */
	while(( charRead = fis.read() ) != -1) 
	{ 
		System.out.print((char)charRead); 
	} 

	/*The close() methodd also throws a checked exception: IOException*/
	fis.close(); 	
   }
}


Handling using try/catch


This is the better option of exception handling as we handle the exception with meaningful message.

  1. import java.io.*;
  2. class FileIOExample {  
  3.    public static void main(String args[])
  4.    {
  5.     FileInputStream fis = null;
  6.     try{
  7.     fis=new FileInputStream("C:/test.txt")
  8.     }
  9.     catch(FileNotFoundException fnfe){
  10.             System.out.println("File is not " +
  11.             "present at the specified path");
  12.      }
  13.    
  14.     int charRead=0;
  15.  
  16.     try{
  17.     while(( charRead = fis.read() ) != -1)
  18.     {
  19.         System.out.print((char)charRead);
  20.     }
  21.     fis.close();
  22. }
  23. catch(IOException ioe){
  24.         System.out.println("I/O exception occurred while reading file: "+ioe);
  25.      }
  26.    }
  27. }
import java.io.*;
class FileIOExample {  
   public static void main(String args[])
   {
	FileInputStream fis = null;
	try{
	fis=new FileInputStream("C:/test.txt")
	}
	catch(FileNotFoundException fnfe){
            System.out.println("File is not " +
			"present at the specified path");
	 }
    
	int charRead=0; 

	try{
	while(( charRead = fis.read() ) != -1) 
	{ 
		System.out.print((char)charRead); 
	} 
	fis.close(); 
}
catch(IOException ioe){
	    System.out.println("I/O exception occurred while reading file: "+ioe);
	 }
   }
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on