Professional Documents
Culture Documents
Exception Handling: Hierarchy of Java Exception Classes
Exception Handling: Hierarchy of Java Exception Classes
Exception is an event that disrupts the normal flow of the program. It is an object which is
thrown at runtime.
The exception handling in java is one of the mechanisms to handle the runtime errors so
that normal flow of the application can be maintained.
There are mainly two types of exceptions: checked and unchecked where error is
considered as unchecked exception. The sun microsystem says there are three types of
exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
The classes that extend Throwable class except RuntimeException and Error are known
as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are
checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time rather they are checked at
runtime.
3) Error
1. int a=50/0;//ArithmeticException
If we have null value in any variable, performing any operation by the variable occurs an
NullPointerException.
1. String s=null;
2. System.out.println(s.length());//NullPointerException
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException. Suppose I have
a string variable that have characters, converting this variable into digit will occur
NumberFormatException.
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException
If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
1. try
2. catch
3. finally
4. throw
5. throws
Explanation:
The code piece which you feel can throw exception need to be enclosed in try
block.
The catch block can contain things which need to be done after catching an
exception
Without Exception handling
public class Ex1{
public static void main(String args[]){
int data=50/0;
System.out.println("rest of the code...");
}
}
OutPut:
Exception in thread main java.lang.ArithmeticException:/ by zero
System.out.println(e);
}
System.out.println("rest of the code...");
}
}
OutPut:
Explanation: In above code the catch block has caught the exception and the flow of execution is
not broken. Even there is an exception System.out.println("rest of the code..."); line got
executed. This is the importance of exception handling in JAVA.
Single try block with multiple catch blocks:
try{
int data=50/0;
}
catch(NullPointerException n) {
System.out.println("something is null");
}
catch(ArrayIndexOutOfBoundsException a) {
catch(NumberFormatException x) {
System.out.println("number format exception");
}
finally{
At a time only one Exception is occurred and at a time only one catch block is executed.
All catch blocks must be ordered from most specific to most general i.e. catch for
ArithmeticException must come before catch for Exception.
What if the catch blocks are not ordered from most specific to general?
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e){System.out.println("common task completed");}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
Explanation: In above example the first catch block is capable of handling all exception hence the
next catch blocks are unreachable. It’s a dead code.
Nested Try blocks Syntax:
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
Example:
class Ex4{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e){System.out.println(e);}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
System.out.println("other statement);
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
}
If you don't handle exception, before terminating the program, JVM executes finally block
(if any).
Difference between throw and throws in Java
There are many differences between throw and throws keywords. A list of differences
below:
1) Java throw keyword is used to Java throws keyword is used to declare an exception.
explicitly throw an exception.
4) Throw is used within the Throws is used with the method signature.
method.
5) You cannot throw multiple You can declare multiple exceptions e.g.
exceptions. public void method()throws IOException,SQLException.