Introduction
Exception handling is very critical to any application and any language that was used to develop. This helps the following stakeholders
Stakeholders
End-User:
A graceful message that indicates that something has gone wrong in the system due to some wrong input or due to some internal fault
Developer
A clean way to get to know which line of code caused the exception and the stack trace that can help us reach to the points impacted
Support Team
A helpful hint that they can try to verify and resolve if it is something within their reach or to escalate to the product team
SRE (Site Reliability Engineering)
An indication of how far the application is performing w.r.to being reliable to the end-users and how faults are handled or tolerated etc..
Problem Statement
When we use threads to get the job done for performing some intensive task (computation or Network intensive tasks), it becomes difficult to track down on the exceptions.
This post is one in a series of posts that will talk about the exception handling strategies w.r.to the threading and tasks in Java.
Let us consider the below given simple app that has multiple threads running tasks. Here, the task is to throw exception which is for demonstration, which can be correlated in realtime with any 3rd party service / external service invocation that might throw exceptions.
package com.ds.mt.exceptions.handling;
public class ExceptionLeaking {
public static void main(String[] args) {
try {
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
} catch (Exception e) {
//this catch block does not get executed as the exceptions are thrown inside threads (not the main thread)
System.out.println("Executing the handler for the thread exceptions");
e.printStackTrace();
}
}
}
class ExceptionLeakingTask implements Runnable {
@Override
public void run() {
throw new RuntimeException("Hello World!!!");
}
}
As mentioned in the comments above, the exceptions are raised in the task that is run inside a thread and the handler is in the main method that wraps all the threads.
However, in runtime, the exception handling catch block does not get executed as the exceptions are not thrown in the main thread, which causes the application to break irrespective of having try...catch.. blocks in place.
Solution
package com.ds.mt.exceptions.handling;
public class ExceptionLeaking {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler("DEFAULT"));
// Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.out.println("Exception raised from : " + t.getName() + " and exception is: " + e.getMessage()));
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
}
}
class ThreadExceptionHandler implements Thread.UncaughtExceptionHandler {
private final String instanceId;
public ThreadExceptionHandler(String instanceId) {
this.instanceId = instanceId;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Exception raised from : " + t.getName() + " inside instance: " + instanceId + " and exception is: " + e.getMessage());
}
}
class ExceptionLeakingTask implements Runnable {
@Override
public void run() {
throw new RuntimeException("Hello World!!!");
}
}
In the above sample code, we have setup a exception handler and wired it to the Thread class Exceptiohandler setter method so that when there is an exception in any of the thread's (all of the threads in the applicaiton), this handler gets invoked.
The handler also catches the exceptions that are thrown in the main method too... figuring out how... obviously its too a thread (A Thread with a name as "main").
Comments
Post a Comment