How to set UncaughtExceptionHandler for a thread group

2.2k views Asked by At

Suppose I have 5 threads belonging to a thread group named "Fruits-group".

How can I assign UncaughtExceptionHandler to all threads of Fruits-group at one go?

I know we can define a global UncaughtExceptionHandler for all threads. What I am looking for is assigning a UncaughtExceptionHandler to an entire thread-group?

1

There are 1 answers

2
Andreas On BEST ANSWER

TL;DR Subclass the ThreadGroup and override the uncaughtException() method.

A ThreadGroup is an UncaughtExceptionHandler, implementing the uncaughtException(Thread t, Throwable e) method:

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

The uncaughtException method of ThreadGroup does the following:

  • If this thread group has a parent thread group, the uncaughtException method of that parent is called with the same two arguments.
  • Otherwise, this method checks to see if there is a default uncaught exception handler installed, and if so, its uncaughtException method is called with the same two arguments.
  • Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.

Applications can override this method in subclasses of ThreadGroup to provide alternative handling of uncaught exceptions.


UPDATE

If you want to be able to set an UncaughtExceptionHandler for the ThreadGroup, you can create a delegating subclass:

public class ExceptionHandlingThreadGroup extends ThreadGroup {
    private UncaughtExceptionHandler uncaughtExceptionHandler;
    
    public ExceptionHandlingThreadGroup(String name) {
        super(name);
    }
    public ExceptionHandlingThreadGroup(ThreadGroup parent, String name) {
        super(parent, name);
    }
    
    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return this.uncaughtExceptionHandler;
    }
    public void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
        this.uncaughtExceptionHandler = uncaughtExceptionHandler;
    }
    
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        if (this.uncaughtExceptionHandler != null)
            this.uncaughtExceptionHandler.uncaughtException(t, e);
        else
            super.uncaughtException(t, e);
    }
}

In general, though, it would likely be better to just implement the exception handling logic directly in the subclass, but this way, you can use an existing UncaughtExceptionHandler implementation.