Multi-Catch Exception Example

This program demonstrates handling multiple exceptions in a single catch block.

Problem Statement

Write a Java program that uses multi-catch to handle both ArithmeticException and ArrayIndexOutOfBoundsException.

Source Code

1 public class MultiCatchExample {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Multi-Catch Example");
4 try {
5 int[] arr=new int[3];
6 int result=arr[5]/0;
7 System.out.println("Eduinq Result: "+result);
8 } catch(ArithmeticException | ArrayIndexOutOfBoundsException e) {
9 System.out.println("Eduinq Exception: "+e.getClass().getSimpleName());
10 }
11 }
12 }

Program Output

					Eduinq Multi-Catch Example
Eduinq Exception: ArrayIndexOutOfBoundsException
				

Explanation

Multi-catch allows handling multiple exceptions in one block. - Reduces code duplication and improves readability. - Exception type is identified dynamically. - In enterprise systems, multi-catch simplifies error handling for related exceptions. Conclusion: Shows multi-catch exception handling.

Quick Links to Explore