Monday 14 November 2016

Nested try Statement



    There may be situations where there is a possibility of generation of multiple exceptions of different types within a particular block of program code. In such situations multiple try statements can be used.
    Example:
classMultitry
{
public static void main(String args[])
 {
int r;
int a[]={10,20};
try
  {
a[2]=30;
try
    {
     r=a[0]/(a[1]-20);
    }
catch(ArithmeticException e)
    {
System.out.println("Arithmetic exception occurred");
    }
   }
catch(ArrayIndexOutOfBoundsException e)
  {
System.out.println("Array indexing exception occurred");
  }
  r=a[0]+a[1];
System.out.println("Result = "+r);
 }
}
Output:
Array indexing exception occurred
Result = 30

No comments:
Write comments