Multiple Exception Catching

Ads
 

Multiple Exception Catching

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

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

Ads

Ads