Home » Java programming language

Java Thread Class static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() method with Example

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

Thread Class static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

  • This method is available in package java.lang.Thread.getDefaultUncaughtExceptionHandler().
  • This method is used to return 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 Thread.UncaughtExceptionHandler, it provides the default handler to handle the uncaught exception.
  • 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 it does not raise any exception that means normal termination of the thread.

Syntax:

    static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
    }

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 default handler for uncaught exception else null if there is no default means normal termination.

Java program to demonstrate example of getDefaultUncaughtExceptionHandler() 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 DefaultExceptionHandler extends Thread {
    // 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());
    }

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

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

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

        /*  getDefaultUncaughtExceptionHandler() will return 
            the default handler for uncaught exception and 
            create a reference of Thread.UncaughtExceptionHandler
        */
        Thread.UncaughtExceptionHandler ueh = Thread.getDefaultUncaughtExceptionHandler();
        System.out.println("The Default handler for the thread is = " + ueh);
    }
}

Output

E:\Programs>javac DefaultExceptionHandler.java

E:\Programs>java DefaultExceptionHandler
The Default handler for the thread is = null
The name of this thread is  Thread-1
The name of this thread is  Thread-2





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.