Thread setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler) in Java

setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler): This method is available in the java.lang.Thread class of Java.

Syntax:

void java.lang.Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)

This method takes one argument. This method set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

Uncaught exception handling is controlled first by the thread, then by the thread's ThreadGroup object, and finally by the default uncaught exception handler.

Parameters: One parameter is required for this method.

eh: the object to use as the default uncaught exception handler. If null then there is no default handler.

Returns: NA

Throws:

1. SecurityException - if a security manager is present and it denies RuntimePermission("setDefaultUncaughtExceptionHandler").

Approach

Java

package com.Thread;

import java.lang.Thread.UncaughtExceptionHandler;

public class ThreadsetDefaultUncaughtExceptionHandler {
    public static void main(String[] args) {

        String name = "hello";
        UncaughtExceptionHandler eh = new ThreadGroup(name);
        Thread.setDefaultUncaughtExceptionHandler(eh);
        System.out.println("setDefaultUncaughtExceptionHandler");
    }
}

Output:

setDefaultUncaughtExceptionHandler


Some other methods of Thread

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

Thread.holdsLock(Object)This method returns true if and only if the current thread holds the monitor lock on the specified object.

interrupt()This method interrupts this thread.

Thread.interrupted()This method tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method.

isAlive()This method tests if this thread is alive. A thread is alive if it has been started and has not yet died.

isDaemon()This method tests if this thread is a daemon thread.

isInterrupted()This method tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.

join()This method waits for this thread to die.

join(long)This method waits at most millis milliseconds for this thread to die. A timeout of 0 means waiting forever.

join(long, int)This method waits at most millis milliseconds plus nanos nanoseconds for this thread to die. If both arguments are 0, it means to wait forever.

Thread.onSpinWait()This method indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities.

run()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

setContextClassLoader(ClassLoader)This method sets the context ClassLoader for this Thread.

setDaemon(boolean)This method marks this thread as either a daemon thread or a user thread.

setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler)This method set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

setName(String)This method changes the name of this thread to be equal to the argument name.

setPriority(int)This method changes the priority of this thread.

setUncaughtExceptionHandler(UncaughtExceptionHandler)This method set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Thread.sleep(long)This method causes the currently executing thread to sleep for a specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Thread.sleep(long, int)This method causes the currently executing thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

start()This method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

toString()This method returns a string representation of this thread, including the thread's name, priority, and thread group.

Thread.yield()This method is a hint to the scheduler that the current thread is willing to yield its current use of a processor. 

No comments:

Post a Comment