Thread getUncaughtExceptionHandler() method in java

Let’s learn thread getUncaughtExceptionHandler() method in java.

Thread getUncaughtExceptionHandler() method in java

getUncaughtExceptionHandler() method returns the handler invoked when this thread abruptly terminates due to an uncaught exception.

If this thread has not had an uncaught exception handler explicitly set then this thread’s ThreadGroup object is returned, unless this thread has terminated, in which case null is returned.

Syntax

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

Returns

the uncaught exception handler for this thread

Example

class GetUncaughtExceptionHandlerDemo implements Runnable
{
   Thread t;
	
   public GetUncaughtExceptionHandlerDemo() 
   {
      t = new Thread(this);
      t.start();
   }
   public void run() 
   {
      System.out.println("Current thread name : " + t.getName());
      Thread.UncaughtExceptionHandler handler = t.getUncaughtExceptionHandler();
      System.out.println(handler);
   }

   public static void main(String[] args) 
   {
      new GetUncaughtExceptionHandlerDemo();
      new GetUncaughtExceptionHandlerDemo();
   }
}

Output:

Current thread name : Thread-0
Current thread name : Thread-1
java.lang.ThreadGroup[name=main,maxpri=10]
java.lang.ThreadGroup[name=main,maxpri=10]


Also read – abstraction in java