Home » Java programming language

Java Thread Class static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler) method with Example

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

Thread Class static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler)

  • This method is available in package java.lang.Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler).
  • This method is used to set the Default handler called if any of the thread terminates abnormally if any exception raises and when we didn’t write any code to handle the uncaught exception.
  • This method is static so this method is accessible with the class name too.
  • The return type of this method is void so it does not return anything.
  • This method takes one parameter (Thread.UncaughtExceptionHandler excep_handler) it is the object to use as a default handler to handle the uncaught exception and return null if no other default handler.
  • This method is best suitable if we forgot to write a code of uncaught exceptions so it is the default handler call automatically if a thread terminates abruptly.
  • This method returns null if there is no other default handler.

Syntax:

    static void setDefaultUncaughtExceptionHandler
        (Thread.UncaughtExceptionHandler excep_handler){    
    }

Parameter(s):

We pass only one object as a parameter in the method that is (Thread.UncaughtExceptionHandler excep_handler), so it is the object to use as a default handler for uncaught exception else null if there is no default handler defined.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of setDefaultUncaughtExceptionHandler() 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 DefaultUncaughtExceptionHandlerClass extends Thread {
    // Override run() of Thread class
    public void run() {
        throw new RuntimeException();
    }
}

class Main {
    public static void main(String[] args) {
        // Creating an object of DefaultUncaughtExceptionHandlerClass class

        DefaultUncaughtExceptionHandlerClass uehc =
            new DefaultUncaughtExceptionHandlerClass();

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

        /*  setDefaultUncaughtExceptionHandler
            (Thread.UncaughtExceptionHandler excep_handler) 
            will set the handler for uncaught exception when 
            this thread terminate abnormally 
        */
        th.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread th, Throwable ex) {
                System.out.println(th + " throws exception " + ex);
            }
        });
        th.start();
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
Thread[Thread-1,5,main] throws exception java.lang.RuntimeException


Comments and Discussions!

Load comments ↻





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