Home » Java programming language

Java Thread Class public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler) method with Example

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

Thread Class public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler)

  • This method is available in package java.lang.Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler).
  • This method is used to set 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 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 handler object to use when this thread terminates abnormally due to an uncaught exception.
  • This method return null if this thread has no explicit handler.

Syntax:

    public void setUncaughtExceptionHandler
        (Thread.UncaughtExceptionHandler excep_handler){
    }

Parameter(s):

We pass only one object as a parameter in the method of the Thread and the parameter is the object to use when this thread uncaught exception handler and if null then our thread has no explicit handler.

Return value:

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

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

class Main {
    public static void main(String[] args) {
        // Creating an object of UncaughtExceptionHandlerClass class
        UncaughtExceptionHandlerClass uehc =
            new UncaughtExceptionHandlerClass();
        // Creating an object of Thread class
        Thread th = new Thread(uehc);
        // setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler excep_handler)
        // will set the handler for uncaught exception when 
        // this thread terminate abnormally
        th.setUncaughtExceptionHandler(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

Ad: Are you a blogger? Join our Blogging forum.




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.