0% found this document useful (0 votes)
15 views4 pages

UNIT 3 Session 2

The document provides an overview of multiple catch blocks in Object Oriented Programming, specifically for handling different types of exceptions in Java. It outlines the rules for using multiple catch blocks, including the requirement to order them from most specific to most general. An example code snippet is included to illustrate the implementation of multiple catch blocks in a try-catch structure.

Uploaded by

gnanavel.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

UNIT 3 Session 2

The document provides an overview of multiple catch blocks in Object Oriented Programming, specifically for handling different types of exceptions in Java. It outlines the rules for using multiple catch blocks, including the requirement to order them from most specific to most general. An example code snippet is included to illustrate the implementation of multiple catch blocks in a try-catch structure.

Uploaded by

gnanavel.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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");
• }
• }

You might also like