KGiSL Institute of Technology
(Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai)
Recognized by UGC, Accredited by NBA (IT)
365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.
Department of Computer Science and Engineering
Name of the Faculty : Gnanavel M
Subject Name & Code : 24UCS312 - OBJECT ORIENTED PROGRAMMING
Branch & Department : BE.CSE
Year & Semester : II Year / III Sem
Academic Year :2025-2026
Multiple catch blocks
• Multiple catch is used to handle many different kind of exceptions
that may be generated while running the program. i.e more than one
catch clause in a single try block can be used.
•
• Rules:
• At a time only one Exception can occur and at a time only one catch block is
executed.
• All catch blocks must be ordered from most specific to most general
i.e. catch for ArithmeticException must come before catch for Exception.
•
Syntax
• Syntax:
•
• try {
• // Code block
• }
• catch (ExceptionType1 e1) {
• // Handle ExceptionType1 exceptions
• }
• catch (ExceptionType2 e2) {
• // Handle ExceptionType2 exceptions
• }
• Example:
•
• public class MultipleCatchBlock2 { public static void main(String[] args) {
• try
• {
• int a[]= {1,5,10,15,16};
• System.out.println("a[1] = "+a[1]);
• System.out.println("a[2]/a[3] = "+a[2]/a[3]);
• System.out.println("a[5] = "+a[5]);
• }
• catch(ArithmeticException e)
• {
• System.out.println("Arithmetic Exception occurs");
• }
• catch(ArrayIndexOutOfBoundsException e)
• {
• System.out.println("ArrayIndexOutOfBounds Exception occurs");
• }
• catch(Exception e)
• {
• System.out.println("Parent Exception occurs");
• }
• System.out.println("rest of the code");
• }
• }
•