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

Write about Exception Handling concept in Java with Example?

Exception: An exception is a condition that is caused by a run –time error in the program. when the java interpreted encounters an error such as dividing an integer by zero. It creat...

Write about Different Types of Errors Occured in Java?

Types of Errors :  Errors may be classified in to 2 categories 1)Compile time errors 2)Run time errors Compile Time Errors: All syntax errors will be detected an...

Differences between while and do–while loops

While Loop: It is a looping construct that will execute only it the test condition is true. It is an entry-controlled loop. It is used for implementing common loopin...

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