Saturday, February 28, 2009

Exception Handling:try-catch part2

Mechanism of exception Handling:-

In java ,the exception handling mechanism handles abnormal and unexpected situations in a structured manner.
As i told in previous tutorial when an exception is raised JRE searches for an exception handler (try-catch)
in the method that caused exception.If handler not found there then it searches in calling method.
The search goes on until The runtime system finds an appropriate exception handler ,that is situation where
the type of exception caught by the handler is the same as the type of exception thown.

Detailed Example:-

//Except.java

public class Except{
public static void main(String a[]){
int result=0,num1=10,num2=0;
try
{
result=num1/num2;
}catch(Exception e)
{
System.out.println("Error divide by zero");
}
System.out.println("Result is:"+result);
}
}

Explanation:-
In above mentioned code the main method contains both try and catch.
Since exception is raised in main thus first the runtime system will
check main for exception handler,if it is there in this case present
in same method main then it is fine displays message.

Multiple catch blocks:-

A single try block may have many catch blocks.This is necessary when the try block has statements that
may result in different types of exceptions.Following code show Three type of exceptions to be trapped.

//Multiplecatch.java

public class Multiplecatch{
public static void main(String a[])
{
int number[]={0,0};
int num1,num2,result=0;
num1=10;
num2=0;
try
{
result=num1/num2;
System.out.println(num1/number[2]);
}
catch(ArithmeticException e){
System.out.println("Error divide by zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error out of bounds");
}
catch(Exception e){}
System.out.println("result is:"+result);
}
}

Output:-Error divide by zero
result is:0

Explanation:-

In code above try has two statements that may result in many exceptions.
Here 3 catch blocks are following try block.

NOTE:-
1.The catch block that has the most specific exception class must be written first.
2.The subclass comes first than superclass that is ArithmeticException comes first than Exception class.
because Exception is capable of handling all exceptions raised thus resulting in compiler error.
3.This will result in an compiler error stating Unreachable code.
4.Whenever an exception is raised on a particular statement ,the next statement will never get executed due to program flow interruption.

Nested try-catch:-
In nested try-catch block we have one try-catch block inside another.Similarly a catch block can contain try-catch block.If lower level try-catch block does not have matching catch handler ,the outer try block is checked for it.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP