Checked & Unchecked Exception

Checked Exception

Checked exceptions are already identified in java. These exception classes are child of Exception class. Checked exception has to be handled.

Important ! If program is generating a checked exception then that program must handle that exception. It means exception handling mechanism must be included in program, Otherwise compiler will generate error.

List of Checked Excetion

  1. IOException
  2. FileNotFoundException
  3. ParseException
  4. ClassNotFoundException
  5. CloneNotSupportedException
  6. InstantiationException
  7. InterruptedException
  8. NoSuchMethodException
  9. NoSuchFieldException

All above listed exception are checked exceptions. If program is generating these exception then it's programmer responsibility to handle it or explicitly specify that program can generate exception. throws keyword can be used to specifying. for example say in main method we are trying to open a stream with a file.

Program :


import java.io.*;
class file
{
public static void main(String arg[])
{
FileInputStream fis=new FileInputStream("one.txt");
}
}

If we try to execute above piece of code then it will show following error:

Error two.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
FileInputStream fis=new FileInputStream("one.txt");
^
1 error

Reason for the error in above code is unhandling of checked exception. unreported exception FileNotFoundException; must be caught or declared to be thrown , specified in error as well that it must be caught or declared to be thrown.

So now we will specify that main method can generate an exception using throws keyword.

Program :


import java.io.*;
class file
{
public static void main(String arg[]) throws FileNotFoundException
{
FileInputStream fis=new FileInputStream("one.txt");

}
}

Now compiler will allow compilation of above code. Throws keyword is used to specify what exception program can generate so that programmer who is going to use it, can handle it.

This program can generate "FileNotFoundException" which is checked exception. It means whether it must be handled or must be specified that main method can generate exception.



Unchecked Exception

Unchecked exception are not checked during compilation of program. If there is any unchecked exception in the program then it won't create any problem for compilation.

Following are the unchecked exception list:

  1. ArrayIndexOutOfBoundsException
  2. ClassCastException
  3. IllegalArgumentException
  4. IllegalStateException
  5. NulPointerException
  6. NumberFormatException
  7. AssertionError
  8. ExceptionInInitializerError
  9. StackOverflowError
  10. NoClassDefFoundError

Following code trying to access element stored at 4th index, but array having total 4 element. It means indices are from 0 to 3. Program will generate ArrayIndexOutOfBoundException , which is unchecked exception. Compilation of below program will be done without any error.

Program :


 class exp
{
public static void main(String arg[])
{
int b[]={1,2,30,12};
System.out.println("a:"+b[4]);
System.out.println("End of program");
}
}