Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to use catch block to handle chained exceptions?

Program to demonstrate multiple catch blocks handling chained exceptions in Java


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 : 

© 2021 Learn Java by Examples Template by Hubberspot