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
- 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();
- }
- }
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.
- 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();
- }
- }
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.
- 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);
- }
- }
- }
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); } } }