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?
TL;DR Subclass the
ThreadGroup
and override theuncaughtException()
method.A
ThreadGroup
is anUncaughtExceptionHandler
, implementing theuncaughtException(Thread t, Throwable e)
method:UPDATE
If you want to be able to set an
UncaughtExceptionHandler
for theThreadGroup
, you can create a delegating subclass: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.