๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Multiple catch Block in Java  


Introduction

  • In previous topics, we have learned about try-catch block which is used to handle exceptions in Java.
    • Also we have learned that we can handle only one type of exception in a single catch block.
  • But what if we want to handle multiple types of exceptions that can be thrown by the try block?
  • Here comes the concept of multiple catch blocks.
  • Definition:
    • A multiple catch block in Java is a structure where a single try block is followed by more than one catch block, each designed to handle a different type of exception.
    • This allows a program to respond differently depending on the exception that occurs.
  • Syntax:
    • try
      {
          // Risky code that may throw an exception
      }
      catch(ExceptionType1 ref_variable1)
      {
          // Code to handle ExceptionType1
      }
      catch(ExceptionType2 ref_variable2)
      {
          // Code to handle ExceptionType2
      }
      ...
  • Example:
    • import java.util.Scanner;
      import java.util.InputMismatchException;
      
      public class MainApp
      {
          public static void main(String[] args)
          {
              System.out.println("----- App Started -----");
      
              Scanner sc = new Scanner(System.in);
      
              try
              {
                  System.out.println("Enter no 1");
                  int no1 = sc.nextInt();
      
                  System.out.println("Enter no 2");
                  int no2 = sc.nextInt();
      
                  int res = no1 / no2;
                  System.out.println("Result : "+res);
              }
              catch(InputMismatchException ime)
              {
                  System.out.println("Input Mismatch Exception Occured : "+ime);
              }
              catch(ArithmeticException ae)
              {
                  System.out.println("Arithmetic Exception Occured : "+ae);
              }
              System.out.println("----- App Finished Successfully -----");
          }
      }
      • Here we have used two catch blocks to handle two different types of exceptions:
      • First catch block handles InputMismatchException which may occur if the user enters non-integer input.
      • Second catch block handles ArithmeticException which may occur if the user tries to divide by zero.

Points to remember for Multiple catch block:
  1. Each catch block handles one specific exception type.
    • For example, in the above example, the first catch block handles InputMismatchException and the second catch block handles ArithmeticException.
  2. Only the first matching catch block is executed when an exception occurs.
    • For example, if an InputMismatchException occurs, only the first catch block will be executed and the second catch block will be skipped.
  3. The order of catch blocks is important:
    • If the first catch block has a parent class exception (like Exception), then the next catch block cannot have a child class exception (like ArithmeticException).
    • This is because the parent class catch block will catch all exceptions, so the child class block will never execute.
    • Example of incorrect order:
      try {
          int num = 10 / 0;
      } 
      catch (Exception e)             // Parent class
      {
          System.out.println("Exception caught");
      } 
      catch (ArithmeticException ae)  // Child class
      {
          System.out.println("Arithmetic Exception caught");
      }
    • Example of correct order (Child first, Parent later):
      try
      {
          int num = 10 / 0;
      } 
      catch (ArithmeticException ae)      // Child class
      {
          System.out.println("Arithmetic Exception caught");
      } 
      catch (Exception e)                 // Parent class
      {
          System.out.println("Exception caught");
      }