Pin It

How to use catch block to handle chained exceptions?

Program to demonstrate multiple catch blocks handling chained exceptions in Java

import java.io.IOException;


public class ChainedException {

   public static void main(String[] args) {

      int i = 10;
      try {
 i = i / 0;
 System.out.println("In First try block");
      }catch(ArithmeticException ae) {
 System.out.println("In First Catch Block");
 System.out.println("Arithmetic Exception Handled :\n" + ae);
 
        try {
    throw new IOException();
 }catch(IOException ioe) {
    System.out.println("In Second Catch Block");
    System.out.println("IOException Handled :\n" + ioe);
    try {
       throw new NumberFormatException();
    }catch(NumberFormatException nfe) {
       System.out.println("In Third Catch Block");
       System.out.println("NumberFormatException Handled  :\n" + nfe);
    }

  }

      }
   }
}



Output of the program : 




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot