Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Exception Handling

 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.

Hierarchy of Java Exception classes


Types of Exception

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

Difference between checked and unchecked exceptions


1) Checked Exception

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

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Common scenarios where exceptions may occur


1) Scenario where ArithmeticException occurs

If we divide any number by zero, there occurs an ArithmeticException.

1. int a=50/0;//ArithmeticException

2) Scenario where NullPointerException occurs

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

4) Scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:

1. int a[]=new int[5];


2. a[10]=50; //ArrayIndexOutOfBoundsException

Java Exception Handling Keywords

There are 5 keywords used in java exception handling.

1. try
2. catch
3. finally
4. throw
5. throws

Java try block


 Java try block is used to enclose the code that might throw an exception. It must
be used within the method.
 Java try block must be followed by either catch or finally block.

Java catch block


 Java catch block is used to handle the Exception. It must be used after the try
block only.
 You can use multiple catch block with a single try.

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

Explanation: In above code when program starts execution it throws exception at


int data=50/0; because of which the execution is stopped and the next line is not
executed.

With Exception handling


public class Ex1{
public static void main(String args[]){
try {
int data=50/0;
}
catch(ArithmeticException e)
{

System.out.println(e);

}
System.out.println("rest of the code...");
}
}

OutPut:

Exception in thread main java.lang.ArithmeticException:/ by zero


rest of the code...

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) {

System.out.println("check the index");


}
catch(ArithmeticException z) {

System.out.println("divide by zero error");


}

catch(NumberFormatException x) {
System.out.println("number format exception");

}
finally{

System.out.println("prog ends here");


}

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");}

System.out.println("rest of the code...");


}
}

Output: Compile-time error

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

Java finally block


 Java finally block is a block that is used to execute important code such as
closing connection, stream etc.
 Java finally block is always executed whether exception is handled or not.
 Java finally block must be followed by try or catch block.

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:

No. Throw Throws

1) Java throw keyword is used to Java throws keyword is used to declare an exception.
explicitly throw an exception.

2) Checked exception cannot be Checked exception can be propagated with throws.


propagated using throw only.

3) Throw is followed by an Throws is followed by class.


instance.

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.

Java throw example


1. void m(){
2. throw new ArithmeticException("sorry");
3. }

Java throws example


1. void m()throws ArithmeticException{
2. //method code
3. }

Java throw and throws example


1. void m()throws ArithmeticException{
2. throw new ArithmeticException("sorry");
3. }

You might also like