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

Explain Some Commonly used String Methods

S2 = S1 . to lowercase: Converts the string S1 to all lower case. S2 = S1 to upper case: Converts the string S1 to all upper S2 = S1. Replace (...

What are the Benefits of Organizing Classes in to Packages

1. The classes contained in the packages of other programs can be easily reused. 2. In packages, classes can be unique compared with classes in other packages. That is, two classes in two differe...

What are Mathematical Functions in Java?

Java supports basic mathematical functions through math class defined in the Java long package. These functions should be used as follows math. Function _ name( ); Example: Double y = math. S...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item