Exception Hierarchy Example

This program demonstrates exception hierarchy in Java.

Problem Statement

Write a Java program that catches exceptions using superclass Exception.

Source Code

1 public class ExceptionHierarchyExample {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Exception Hierarchy Example");
4 try {
5 int result=10/0;
6 } catch(Exception e) {
7 System.out.println("Eduinq Exception: "+e.getClass().getSimpleName());
8 }
9 }
10 }

Program Output

					Eduinq Exception Hierarchy Example
Eduinq Exception: ArithmeticException
				

Explanation

All exceptions inherit from Throwable. - Catching superclass Exception handles all checked and unchecked exceptions. - Provides generic error handling. - In enterprise systems, exception hierarchy ensures flexible error management. Conclusion: Shows exception hierarchy with superclass catch.

Quick Links to Explore