Checked and Unchecked Exception
In this section, you will learn Checked and Unchecked Exceptions in java and how to handle it. The Exception is a condition which indicates error in your code.
Types of built-in exception in Java
Given below types of built-in exception in java :
1. Unchecked Exception
2. Checked Exception.
Unchecked Exception
You don't need to import java.lang package since it is implicitly available in all java programs. The exceptions derived from 'RuntimeException' are readily available. Due to this, these exception need not to include in any methods 'throws' list. These exceptions are called 'Unchecked Exception'. Some of the common examples are : ArithmeticException, NullPointerException, ClassCaseException.
Checked Exception
These Exceptions are defined by 'java.lang' and must be include in a methods
'throws' list. These exceptions are known as Checked Exception.
Given below the complete List of Checked Exception :
- ClassNotFoundException
- CloneNotSupportException
- IllegalAccessException
- InstantiationException
- InterruptedException
- NoSuchFieldException
- NoSuchMethodException
Handling Exception in Java
In java exception is handled through the five keywords : try, catch, throw, throws and finally. The code which you are suspect of can put into try block to monitor for exception. Using catch, you can handle the exception. Each try block need at least one catch or a finally clause. For throwing exception explicitly, we use keyword throw. Any known exception that is thrown out of a method must be specified as such by throws clause. Generally checked exceptions are thrown using throws clause.
try and catch example :
public class TryCatch { public static void main(String args[]) { int d, a; try { d = 0; a = 42 / d; System.out.println("This will not be printed" + a); } catch (ArithmeticException e) { System.out.println("Divided by zero"); } System.out.println("After catch statement"); } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac TryCatch
.java C:\Program Files\Java\jdk1.6.0_18\bin>java TryCatch Divided by zero After catch statement |
Throw :
You can throw exception explicitly as follows :
throw ThrowableInstance ;
ThrowableInstance must be an object of type 'Throwable' or a subclass of 'Throwable'.
Example :
public class ThrowDemo { static void DevProc() { try { throw new NullPointerException("demo"); } catch (NullPointerException e) { System.out.println("Caught inside Devproc."); throw e; } } public static void main(String args[]) { try { DevProc(); } catch (NullPointerException e) { System.out.println("Recaught :" + e); } } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo
.java C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo Caught inside Devproc. Recaught :java.lang.NullPointerException: demo |
throws :
If a method is capable of causing exception but it is not using 'catch' to handle this. The 'throws' specify this behaviour so that callers of the method can guard themselves from that exception.
Example :
public class ThrowsDemo { static void throwone() throws IllegalAccessException { System.out.println("Inside throwone"); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwone(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo
.java C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo Caught inside Devproc. Recaught :java.lang.NullPointerException: demo |
finally
finally creates a block of code that will be executed after a try/catch block, and it will execute whether or not an exception is thrown. This can be useful for closing file handles and freeing up any other resources that might have been allocated.
Note :- The finally clause is optional . However each try block need at least one catch or a finally clause.
Example :
public class FinallyDemo{ public static void main(String args[]){ int x[] = new int[2]; try{ System.out.println("Element three :" + x[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } finally{ x[0] = 6; System.out.println("First element: " +x[0]); System.out.println("The finally statement is executed"); } } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac FinallyDemo.java C:\Program Files\Java\jdk1.6.0_18\bin>java FinallyDemo Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed |