Home Java Jdk7 Multiple Exception Catching

Related Tutorials


 
 

Share on Google+Share on Google+

Multiple Exception Catching

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 2, 2011

Related Tutorials

Discuss: Multiple Exception Catching  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com