
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to handle the exception using UncaughtExceptionHandler in Java?
The UncaughtExceptionHandler is an interface inside a Thread class. When the main thread is about to terminate due to an uncaught exception the java virtual machine will invoke the thread’s UncaughtExceptionHandler for a chance to perform some error handling like logging the exception to a file or uploading the log to the server before it gets killed. We can set a Default Exception Handler which will be called for the all unhandled exceptions. It is introduced in Java 5 Version.
This Handler can be set by using the below static method of java.lang.Thread class.
public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)
We have to provide an implementation of the interface Thread.UncaughtExceptionHandler, which has only one method.
Syntax
@FunctionalInterface public interface UncaughtExceptionHandler { void uncaughtException(Thread t, Throwable e); }
Example
public class UncaughtExceptionHandlerTest { public static void main(String[] args) throws Exception { Thread.setDefaultUncaughtExceptionHandler(new MyHandler()); throw new Exception("Test Exception"); } private static final class MyHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("The Exception Caught: " + e); } } }
Output
The Exception Caught: java.lang.Exception: Test Exception
- Related Articles
- How to handle the Runtime Exception in Java?
- How to handle an exception using lambda expression in Java?\n
- How to handle an exception in JShell in Java 9?
- How to handle Java Array Index Out of Bounds Exception?
- How to handle an exception in Python?
- How to handle Python exception in Threads?
- How to handle an exception in JSP?
- How to handle python exception inside if statement?
- How to use the try-finally clause to handle exception in Python?
- How to handle exception inside a Python for loop?
- How to handle a python exception within a loop?
- How to handle frame in Selenium WebDriver using java?
- How to handle IllegalArgumentException inside an if using Java
- Is it possible to throw exception without using "throws Exception" in java?
- How to handle EOFException in java?

Advertisements