Handling Multiple Exceptions

This program demonstrates handling multiple exceptions.

Problem Statement

Write a Java program that handles both ArithmeticException and ArrayIndexOutOfBoundsException.

Source Code

1 public class MultipleCatchBlocks {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Multiple Catch Blocks Example");
4 try {
5 int[] arr={1,2,3};
6 int result=arr[5]/0;
7 System.out.println("Eduinq Result: "+result);
8 } catch(ArithmeticException e) {
9 System.out.println("Eduinq Exception: Arithmetic Error");
10 } catch(ArrayIndexOutOfBoundsException e) {
11 System.out.println("Eduinq Exception: Array Index Error");
12 }
13 }
14 }

Program Output

					Eduinq Multiple Catch Blocks Example
Eduinq Exception: Array Index Error
				

Explanation

Multiple catch blocks allow handling different exceptions separately. - Accessing invalid array index triggers ArrayIndexOutOfBoundsException. - Division by zero would trigger ArithmeticException. - This design ensures specific error messages for different problems. - In enterprise systems, multiple catch blocks improve debugging and error resolution. Conclusion: Shows handling multiple exceptions with catch blocks.

Quick Links to Explore