What is Exception Occurs in a thread?
When an exception occurs in a thread, the normal flow of execution is disrupted, and the thread may terminate if the exception is not caught and handled within the thread’s run method. Uncaught exceptions in a thread will cause the thread to stop executing, but the main program or other threads will continue to run.
Table of Contents
Key Points Exception Handling in Threads:
- If an exception is thrown within the run method of a thread and is not caught, the thread will terminate.
- The stack trace of the exception is printed to the console.
- The uncaught exception handler for the thread (if set) is invoked
2. Uncaught Exception Handler
- Â Java provides a way to set an uncaught exception handler for a thread or for all threads.
- Â The uncaught exception handler allows the application to handle the exception in a custom way, such as logging the exception or taking corrective actions.
3. Thread Termination
- Â If the exception is caught and handled within the run method, the thread can continue running.
- Â If the exception is not handled, the thread terminates immediately after the exception is thrown.
Example
Below is an example demonstrating what happens when an exception occurs in a thread:
Example with Uncaught Exception
java
class ExceptionThread extends Thread {
public void run() {
try {
System.out.println("Thread is running...");
throw new RuntimeException("An exception occurred in the thread.");
} catch (Exception e) {
System.out.println("Exception caught in thread: " + e.getMessage());
}
System.out.println("Thread is ending...");
}
public static void main(String[] args) {
ExceptionThread thread = new ExceptionThread();
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread ends.");
}
}
Example with Uncaught Exception Handler
java
class ExceptionThreadWithHandler extends Thread {
public void run() {
System.out.println("Thread is running...");
throw new RuntimeException("An exception occurred in the thread.");
}
public static void main(String[] args) {
Thread thread = new ExceptionThreadWithHandler();
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Uncaught exception in thread: " + t.getName() + " - " + e.getMessage());
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread ends.");
}
}
Example with Uncaught Exception:
- ExceptionThread class extends Thread.
- The run() method throws a RuntimeException.
- The exception is caught within the run() method, and a message is printed.
- The thread continues running after handling the exception.
- In the main method, the thread is started, and the main thread waits for the thread to finish using join().
Example with Uncaught Exception Handler
- ExceptionThreadWithHandler class extends Thread.
- The run() method throws a RuntimeException without catching it.
- An uncaught exception handler is set for the thread using setUncaughtExceptionHandler().
- The handler prints a custom message when the exception occurs.
- In the main method, the thread is started, and the main thread waits for the thread to finish using join().