Home » Java programming language

Java Thread Class Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() method with Example

Java Thread Class Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() method: Here, we are going to learn about the Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 21, 2019

Thread Class Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

  • This method is available in package java.lang.Thread.getUncaughtExceptionHandler().
  • This method is used to return the handler called if any of the thread terminates abnormally due to uncaught exception if any exception raises.
  • This method is not static so this method is not accessible with classname too.
  • The return type of this method is Thread.UncaughtExceptionHandler it provides the handler called when the thread terminates abnormally due to an uncaught exception.
  • This method returns null if it does not raise any exception that means normal termination of the thread.

Syntax:

    Thread.UncaughtExceptionHandler getUncaughtExceptionHandler(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is Thread.UncaughtExceptionHandler, it returns the handler for uncaught exception when terminating thread abruptly else null if there is no fault means normal termination.

Java program to demonstrate example of getUncaughtExceptionHandler() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate 
    because it is imported by default
*/

import java.lang.Thread;

class UncaughtExceptionHandlerClass extends Thread {

    static Thread th1;

    // Override run() of Thread class
    public void run() {

        //Display a message for the end user 	
        System.out.println("The name of this thread is " + " " + Thread.currentThread().getName());

        // getUncaughtExceptionHandler() will return the handler 
        // for uncaught exception when thread terminate abnormally 
        // and create a reference of Thread.UncaughtExceptionHandler
        Thread.UncaughtExceptionHandler ueh = th1.getUncaughtExceptionHandler();

        System.out.println("The handler for the thread is = " + ueh);
    }

    public static void main(String[] args) {
        // Creating an object of UncaughtExceptionHandlerClass class
        UncaughtExceptionHandlerClass uehc =
            new UncaughtExceptionHandlerClass();

        // Creating an object of Thread class
        th1 = new Thread(uehc);

        // Thread class start() method will call and it will ultimately 
        th1.start();
    }
}

Output

E:\Programs>javac UncaughtExceptionHandlerClass.java

E:\Programs>java UncaughtExceptionHandlerClass
The name of this thread is  Thread-1
The handler for the thread is = java.lang.ThreadGroup[name=main,maxpri=10]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.