Using Global Exception Handler
This program demonstrates global exception handling with Thread.setDefaultUncaughtExceptionHandler.
Problem Statement
Write a Java program that sets a global handler for uncaught exceptions.
Source Code
| 1 | public class GlobalExceptionHandlerExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Global Exception Handler Example"); |
| 4 | Thread.setDefaultUncaughtExceptionHandler((t,e) -> { |
| 5 | System.out.println("Eduinq Global Handler: "+e); |
| 6 | }); |
| 7 | int result=10/0; // uncaught exception |
| 8 | } |
| 9 | } |
Program Output
Eduinq Global Exception Handler Example Eduinq Global Handler: java.lang.ArithmeticException: / by zero
Explanation
Global handler catches uncaught exceptions across threads. - Provides centralized error reporting. - Improves maintainability and monitoring. - In enterprise systems, global handlers ensure consistent error management. Conclusion: Shows global exception handling with default handler.