Java Example:
package concurrency;import java.util.random;public class main6 { public static void main (String[] args) { mythreadgroup threadgroup = new mythreadgroup ("MyThreadGroup"); task1 task = new task1 (); for (int i = 0; i < 2;i++) { thread t = new thread (ThreadGroup,task); t.start (); } }}class mythreadgroup extends threadgroup{ /* must declare a constructor with arguments, because Threadgroup does not have a default constructor with no arguments */ public Mythreadgroup (String name) { &Nbsp; super (name); } /* Overriding the Uncaughtexception method */ @Override public void Uncaughtexception (thread t, throwable e) { System.out.printf ("the thread %s has thrown an exception\n", T.getid ()); e.printstacktrace (System.out); system.out.printf ("terminating the rest of the threads\n"); interrupt (); }}class task1 implements runnable{ /* in this method, we are going to trigger the aritmethicexception anomaly in order to achieve this goal, * we divide 1000 by a random number, and when the random number generator generates 0, the exception is thrown */ @Override public void run () &NBSP;{&NBSP;&NBSp; int result; random random = new random (Thread.CurrentThread (). GetId ()); while (true) { result = 1000/((int) (random.nextdouble () *1000)); system.out.printf ("%s : %d\n", thread.currentthread (). GetId (), result); if (Thread.CurrentThread (). isInterrupted ()) { system.out.printf ("% D : interrupted\n ", thread.currentthread (). GetId ()); return; &nbsP;} } }}
When a thread throws a non-catching exception, the JVM will look for 3 possible processors for this exception. First, look for the non-catching exception handler for the thread that throws the exception, and if the processor does not exist, the JVM continues to look for the non-catching exception handler for the thread group that this thread is in, which is what this section learns, and if it does not exist, the JVM will look for the default non-catching exception handler. If none of these processors exist, the JVM prints the exception information from the stack to the console and exits the program.
Handling of non-controllable exceptions in thread groups