Java - ThreadGroup uncaughtException() Method with Examples



Description

The Java ThreadGroup uncaughtException() method is 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.

Declaration

Following is the declaration for java.lang.ThreadGroup.uncaughtException() method

public void uncaughtException(Thread t, Throwable e)

Parameters

  • t − This is the thread that is about to exit.

  • e − This is the uncaught exception.

Return Value

This method does not return any value.

Exception

NA

Example 1

The following example shows the usage of ThreadGroup uncaughtException() method in case of child and grandchild ThreadGroup objects. We've created a new class NewThreadGroup which extends ThreadGroup. It has a overridden implementation of uncaughtException() method. Using NewThreadGroup, we're creating a threadgroup object. Then we've created two threads using the child and grandchild threadgroup objects created earlier. After threads are started we're interrupting the flow using interrupt() method calls. Once InterruptedException is handled, we're throwing a RuntimeException which is then eventually handled by uncaughtException() method.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup object
    	 NewThreadGroup threadGroup = new NewThreadGroup("ThreadGroup");
		 
         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread[Thread-0,5,ThreadGroup]...
Starting Thread[Thread-1,5,ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-0,5,ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-1,5,ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted

Example 2

The following example shows the usage of ThreadGroup uncaughtException() method in case of child and grandchild ThreadGroup objects. We've created a new class NewThreadGroup which extends ThreadGroup. It has a overridden implementation of uncaughtException() method. Using NewThreadGroup, we're creating parent, child threadgroup objects. Then we've created two threads using the child and grandchild threadgroup objects created earlier. After threads are started we're interrupting the flow using interrupt() method calls. Once InterruptedException is handled, we're throwing a RuntimeException which is then eventually handled by uncaughtException() method.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread[Thread-0,5,Parent ThreadGroup]...
Starting Thread[Thread-1,5,Child ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-0,5,Parent ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-1,5,Child ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted

Example 3

The following example shows the usage of ThreadGroup uncaughtException() method in case of child and grandchild ThreadGroup objects. We've created a new class NewThreadGroup which extends ThreadGroup. It has a overridden implementation of uncaughtException() method. Using NewThreadGroup, we're creating parent, child and grandchild threadgroup objects. Then we've created two threads using the child and grandchild threadgroup objects created earlier. After threads are started we're interrupting the flow using interrupt() method calls. Once InterruptedException is handled, we're throwing a RuntimeException which is then eventually handled by uncaughtException() method.

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
    	 NewThreadGroup pThreadGroup = new NewThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
    	 NewThreadGroup cThreadGroup = new NewThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a grandchild ThreadGroup for parent ThreadGroup
    	 NewThreadGroup gThreadGroup = new NewThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.toString() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.toString() + "...");
         t2.start();
            
         try {
            Thread.sleep(500);
         } catch(InterruptedException ex) {}
         // interrupt the two threads
         t1.interrupt();
         t2.interrupt();
         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   public void run() {
      try {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" executing...");

         while(true) {
            Thread.sleep(500);
         }
      } catch(InterruptedException e) {
         Thread currThread = Thread.currentThread();
         System.out.print(currThread.getName());
         System.out.println(" interrupted:" + e.toString());

         // rethrow the exception
         throw new RuntimeException(e.getMessage());
      }
   } 
}

class NewThreadGroup extends ThreadGroup {

   NewThreadGroup(String n) {
      super(n);
   }

   NewThreadGroup(ThreadGroup parent, String n) {
      super(parent, n);
   }

   public void uncaughtException(Thread t, Throwable e) {
      System.out.println(t + " has unhandled exception:" + e);
   } 
} 

Output

Let us compile and run the above program, this will produce the following result −

Starting Thread[Thread-0,5,Child ThreadGroup]...
Starting Thread[Thread-1,5,GrandChild ThreadGroup]...
Thread-0 executing...
Thread-1 executing...
Thread-1Thread-0 interrupted:java.lang.InterruptedException: sleep interrupted
 interrupted:java.lang.InterruptedException: sleep interrupted
Thread[Thread-1,5,GrandChild ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
Thread[Thread-0,5,Child ThreadGroup] has unhandled exception:java.lang.RuntimeException: sleep interrupted
java_lang_threadgroup.htm
Advertisements