The process
of handling the exception for converting JVM given message to end-user
understandable message or for stopping abnormal termination of the program is
called exception handling.
Need of Exception Handling –
In
projects, exception is handled –
- To stop abnormal termination and
- To provide user understandable messages when exception is raised, so that user can take decision without developer’s help.
Exception handling procedure –
Exception
handling is a 4 step process –
Step 1 – Preparing exception
object appropriate to the current logical mistake.
Step 2 – Throwing that exception
to the appropriate exception handler.
Step 3 – Catching that exception.
Step 4 – Taking necessary actions
To implement exception handling sun has
given two keywords –
1. try – Keyword try establishes a block to write a code
that causes exceptions and its related statements. Exception causing statement
must be placed in try block to be handled and catch that exception for stopping
abnormal terminations.
2. catch – catch block
is used to catch exceptions those are thrown from its corresponding try block.
It has logic to take necessary action on that caught Exception.
Example –
import
java.util.Scanner;
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc
= new Scanner(System.in);
System.out.println("Enter num1 : ");
int i = sc.nextInt();
System.out.println("Enter num2 : ");
int j = sc.nextInt();
int k = i/j;
System.out.println(k);
}
}
Output –
Enter num1 :
10
Enter num2 :
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at
excep.Exception1.main(Exception1.java:11)
Example –
import
java.util.Scanner;
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc
= new Scanner(System.in);
System.out.println("Enter num1 : ");
int i = sc.nextInt();
System.out.println("Enter num2 : ");
int j = sc.nextInt();
try
{
int k = i/j;
System.out.println(k);
}
catch(ArithmeticException
e)
{
System.out.println("Num2 cannot be zero");
}
}
}
Output –
Enter num1 :
10
Enter num2 :
0
Num2 cannot be zero
Note – There are different
types of exceptions to handle our problem, so what kind of exception is going
to occur then according to that we have to specify the exception type inside
catch block.
Note – We can have multiple
catch blocks followed by one try block. If none of the catch block is able to
handle the exception then it raises a pre-defined exception message. The catch
block executes in a sequence of first block executes first.
Suppose
if we have multiple catch block, and in our program there raise an IOException.
And if the first catch block handles ArithmeticException then it moves to the
next block unless and until it doesn’t find the catch block to handle
IOException. If none of the catch block is able to handle the exception then it
raises an error.
There
is a super class of all exception i.e. Exception.
The catch with Exception class should be the last block of the entire catch
block, else it counters an error. This Exception class can handle any kind of
exception. When we don’t know that our program is going to raise what kind of
exception then we can specify super class exception in the catch block.
Example –
import java.io.IOException;
import
java.util.Scanner;
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc
= new Scanner(System.in);
System.out.println("Enter num1 : ");
int i = sc.nextInt();
System.out.println("Enter num2 : ");
int j = sc.nextInt();
try
{
int k = i/j;
System.out.println(k);
}
catch(NumberFormatException
a)
{
System.out.println("There is an numberformat exception");
}
catch(ArithmeticException
e)
{
System.out.println("Num2 cannot be zero");
}
catch(ArrayIndexOutOfBoundsException
e)
{
System.out.println("There is an array index out of bound
exception");
}
}
}
Output –
Enter num1 :
10
Enter num2 :
0
Num2 cannot be zero
Example –
import
java.util.Scanner;
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc
= new Scanner(System.in);
System.out.println("Enter num1 : ");
int i = sc.nextInt();
System.out.println("Enter num2 : ");
int j = sc.nextInt();
try
{
int k = i/j;
System.out.println(k);
}
catch(NumberFormatException
a)
{
System.out.println("There is an numberformat exception");
}
catch(Exception
e)
{
System.out.println("Num2 cannot be 0");
}
}
}
Output –
Enter num1 :
10
Enter num2 :
0
Num2 cannot be 0
Note – In the above example
even we have not specified ArithmeticException, but super class of Exception is
taking care of it.
Example –
import
java.util.Scanner;
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter num1 : ");
int i = sc.nextInt();
System.out.println("Enter num2 : ");
int j = sc.nextInt();
try
{
int k = i/j;
System.out.println(k);
}
catch(Exception
e)
{
System.out.println("Num2 cannot be 0");
}
catch(NumberFormatException
a)
{
System.out.println("There is an numberformat exception");
}
}
}
Output –
Exception in thread "main" java.lang.Error: Unresolved
compilation problem:
Unreachable catch
block for NumberFormatException. It is already handled by the catch block for
Exception
at
excep.Exception1.main(Exception1.java:21)
Note – catch(Exception e)
should be the last catch block else it raise an error.
INNER TRY/CATCH BLOCK
INNER TRY/CATCH BLOCK
The
exception raised in inner try is
caught by inner catch and the
statements placed after inner try/catch is executed. If the inner catch
parameter is no matched with the exception raised in inner try, that exception
is propagated to outer try and it is caught by the outer catch and the
statements placed after outer try/catch are executed. If outer catch is also
not matched, that exception is propagated to JVM and program execution is
terminated abnormally.
Example –
public class
Exception1 {
public static void
main(String[] args) {
Scanner sc
= new Scanner(System.in);
System.out.println("Enter number : ");
int num
= sc.nextInt();
int a[] = new int[num];
try
{
System.out.println("The value of num is : "+num);
try
{
for(int i=0;i<=num;i++)
{
System.out.println(a[i]=i);
}
}
catch(NullPointerException
e)
{
System.out.println("NPE exception");
}
}
catch(ArrayIndexOutOfBoundsException
e)
{
System.out.println("Outer catch handles the inner try
exception");
}
}
}
Output –
Enter number :
5
The value of num is : 5
0
1
2
3
4
Outer catch handles the inner try exception
Note – As the loop inside inner
try block goes from 0 to 5, but the size of an array is 5, so the loop stores 6
values so there raise an exception. As the inner catch cannot handle that
exception, so the outer catch block handles it.
No comments:
Post a Comment