PROGRAM FOR EXCEPTION HANDLING

import java.lang.Throwable; class  Exception {  public static void main(String args[])  {   try                          {  ...

import java.lang.Throwable;
class  Exception
{
 public static void main(String args[])
 {
  try
                         {
                                    System.out.println("Before a");
                                    a();
                                    System.out.println("After a");
  }
                         catch(ArithmeticException e)
 {
                                     System.out.println("Main"+e);
 }
 finally
 {
            System.out.println("Main:finally");
                         }
}
public static  void a()
{
                        try
 {
                                    System.out.println("Before b");
                                    b();
                                    System.out.println("After b");
 }
 catch(ArithmeticException e)
                        {
                                     System.out.println("a:"+e);
 }
 finally
 {
                                     System.out.println("a finally");
 }
}
public static void b()
{
                        try
 {
                                   



int i,a[];
                                    a=new int[3];
                                    System.out.println("Before c");
                                    c();
                                    i=a[4]+a[3];
                                    System.out.println("After c");
 }
 catch(ArrayIndexOutOfBoundsException e)
 {
                                     System.out.println("b:"+e);
 }                     
finally
{
                                    System.out.println("b:finally");
}
}
public static void c()
{
 try
 {
                                    int i=1,j;
                                    j=i/0;
                                    System.out.println("j:"+j);
 }
 catch(ArithmeticException e)
 {
                                    System.out.println("c:"+e);
 }
 finally
 {
                                     System.out.println("c:finally");
}          
 }
}



OUTPUT:

Before a

Before b

Before c

c:java.lang.ArithmeticException: / by zero


c:finally


b:java.lang.ArrayIndexOutOfBoundsException: 4

b:finally

After b

a finally

After a

Main:finally


Related

Java 8186725587985171332

Post a Comment

emo-but-icon

item