Home Java Jdk7 Multiple Exception Catching



Multiple Exception Catching
Posted on: September 2, 2011 at 12:00 AM
In this section, you will learn about new catch method added in Java SE 7 which eliminate duplicate code.

Multiple Exception Catching

In this section, you will learn about new catch method added in Java SE 7 which eliminate duplicate code.

Take a look at the following code :

catch (IOException ex) {
logger.log(ex);
throw ex;
catch (SQLException ex) {
logger.log(ex);
throw ex;
}

Prior to Java SE 7, you can't create common catch method to remove duplicate code. But in Java SE 7, you can create common catch method to remove duplicate code as follows :

public class J7MultipleExceptionCatching
{
public static void main (String args[])
{
int array[]={20,10,30};
int num1=15,num2=0;
int res=0;

try
{
res = num1/num2;
System.out.println("The result is" +res);

for(int ct =2;ct >=0; ct--)
{
System.out.println("The value of array are" +array[ct]);
}

}

catch (ArrayIndexOutOfBoundsException|ArithmeticException e)
{
System.out.println("Error....some exception occur");
}
}
}

OUTPUT

C:\Program Files\Java\jdk1.7.0\bin>javac J7MultipleExceptionCatching.java

C:\Program Files\Java\jdk1.7.0\bin>java J7MultipleExceptionCatching
Error....some exception occur

Download Source Code

Related Tags for Multiple Exception Catching:


More Tutorials from this section

Ask Questions?    Discuss: Multiple Exception Catching  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.