Advertisement
class MultipleCatch
{
public static void main(String... ar)
{
try
{
int arr[]= {1,2,3,4};
System.out.println("Value ="+arr[5]); //ArrayIndexOutOfBoundsException thrown, invalid index 5.
}
catch(ArrayStoreException exp) //catch block to handle/catch ArrayStoreException
{
System.out.println(exp);
}
catch(ArrayIndexOutOfBoundsException exp) //catch block to handle/catch ArrayIndexOutOfBoundsException
{
System.out.println("Exception Caught - "+ exp);
}
catch(Exception exp) //catch block to handle/catch Exception
{
System.out.println(exp);
}
}
}
Exception Caught - java.lang.ArrayIndexOutOfBoundsException: 5
Advertisement
class MultipleCatch2
{
public static void main(String... ar)
{
try
{
int arr[]= {1,2,3,4};
System.out.println("Value ="+arr[5]); //ArrayIndexOutOfBoundsException thrown, invalid index 5.
}
catch(Exception exp) //catch block to handle/catch a more general superclass "Exception"
{
System.out.println("Exception Caught - "+ exp);
}
catch(ArrayIndexOutOfBoundsException exp) //catch block to handle/catch a very specific subclass
//of Exception class, "ArrayIndexOfBoundsException"
{
System.out.println(exp);
}
}
}
MultipleCatch2.java:18: error: exception ArrayIndexOutOfBoundsException has already been caught
catch(ArrayIndexOutOfBoundsException exp) //catch block to handle/catch Exception
^
1 error
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement