Designing Custom Exception Hierarchy
This program demonstrates designing custom exception hierarchy.
Problem Statement
Write a Java program that defines base exception and specialized exceptions.
Source Code
| 1 | class EduinqException extends Exception { |
| 2 | public EduinqException(String msg){ super(msg); } |
| 3 | } |
| 4 | class ValidationException extends EduinqException { |
| 5 | public ValidationException(String msg){ super(msg); } |
| 6 | } |
| 7 | class DatabaseException extends EduinqException { |
| 8 | public DatabaseException(String msg){ super(msg); } |
| 9 | } |
| 10 | public class CustomHierarchyExample { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq Custom Hierarchy Example"); |
| 13 | try { |
| 14 | throw new ValidationException("Invalid Data"); |
| 15 | } catch(EduinqException e) { |
| 16 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 17 | } |
| 18 | } |
| 19 | } |
Program Output
Eduinq Custom Hierarchy Example Eduinq Exception: Invalid Data
Explanation
Custom hierarchy organizes exceptions logically. - Base class provides common handling, subclasses provide specificity. - Improves clarity and maintainability. - In enterprise systems, hierarchy ensures structured error management. Conclusion: Shows custom exception hierarchy design.