uncaughtException(Thread, Throwable): This method is available in the java.lang.ThreadGroup class of Java.
Syntax:
void java.lang.ThreadGroup.uncaughtException(Thread t, Throwable e)
This method takes two arguments. This method is called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandlerinstalled.
The uncaughtException method of ThreadGroup does the following:
1. If this thread group has a parent thread group, the uncaughtException method of that parent is called with the same two arguments.
2. Otherwise, this method checks to see if there is a default uncaught exception handler installed and if so, its uncaughtException method is called with the same two arguments.
3. Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the threads name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.
Parameters: Two parameters are required for this method.
t: the thread that is about to exit.
e: the uncaught exception.
Returns: NA
Approach
Java
package com.ThreadGroup;public class ThreadGroupuncaughtException {public static void main(String[] args) {String name = "hello";ThreadGroup threadGroup = new ThreadGroup(name);Thread t = new Thread();Throwable e = new NullPointerException();threadGroup.uncaughtException(t, e);}}
Output:
Exception in thread "Thread-0" java.lang.NullPointerException at com.ThreadGroup.ThreadGroupuncaughtException.main(ThreadGroupuncaughtException.java:10)
Some other methods of ThreadGroup class
ThreadGroup(String): This method constructs a new thread group. The parent of this new group is the thread group of the currently running thread.
ThreadGroup(ThreadGroup, String): This method creates a new thread group. The parent of this new group is the specified thread group.
activeCount(): This method returns an estimate of the number of active threads in this thread group and its subgroups.
activeGroupCount(): This method returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterates overall subgroups in this thread group.
checkAccess(): This method determines if the currently running thread has permission to modify this thread group.
destroy(): This method destroys this thread group and all of its subgroups. This thread group must be empty, indicating that all threads that had been in this thread group have since stopped.
enumerate(Thread[]): This method copies into the specified array of every active thread in this thread group and its subgroups.
enumerate(ThreadGroup[]): This method copies into the specified array references to every active subgroup in this thread group and its subgroups.
enumerate(Thread[], boolean): This method copies into the specified array every active thread in this thread group.
enumerate(ThreadGroup[], boolean): This method copies into the specified array of references to every active subgroup in this thread group.
getMaxPriority(): This method returns the maximum priority of this thread group.
getName(): This method returns the name of this thread group.
getParent(): This method returns the parent of this thread group.
interrupt(): This method interrupts all threads in this thread group.
isDaemon(): This method tests if this thread group is a daemon thread group.
isDestroyed(): This method tests if this thread group has been destroyed.
list(): This method prints information about this thread group to the standard output.
parentOf(ThreadGroup): This method tests if this thread group is either the thread group argument or one of its ancestor thread groups.
setDaemon(boolean): This method changes the daemon status of this thread group.
setMaxPriority(int): This method sets the maximum priority of the group. Threads in the thread group that already have a higher priority are not affected.
toString(): This method returns a string representation of this Thread group.
uncaughtException(Thread, Throwable): This method is called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandlerinstalled.
No comments:
Post a Comment