Although we do not want to create a program that throws Run-time exceptions when it is not expected, this situation can occur-especially when a complex program is run for the first time. These exceptions are usually handled using the default behavior, the print stack overflow, and the life of the end thread.
Where do I find the default behavior? Each thread belongs to a thread group represented by the Java.lang.ThreadGroup class. As the name suggests, thread groups allow you to group threads together. You may be combining threads for convenience, for example, all threads in one thread pool belong to group X, and all threads in another pool belong to group Y, or they are grouped for access control. The threads in group X do not have permission to access or change threads in group Y unless they are within the same thread group (or within a subgroup).
Before Tiger, the Threadgroup class provided a way to handle an unhandled exception: the Threadgroup uncaughtexception () method. If the exception is not Threaddeath, the name of the thread and stack backtracking (stacks backtrace) are sent to the System.err. But Tiger added another method: the Thread.uncaughtexceptionhandler interface. Subdividing the Threadgroup or installing the implementation of the new interface allows you to change the default behavior. We will study the methods provided before and after Tiger.
customizing behavior using Threadgroup
When an exception occurs that is not caught, the default behavior is to print the stack overflow to the system error (SYSTEM.ERR), as shown in Listing 1. You do not need to use any of the command parameters to start the program.
Listing 1. Example of line Cheng production
public class SimpleDump {
public static void main(String args[]) {
System.out.println(args[0]);
}
}
Running the program without using any parameters will generate the output in Listing 2. Although it is not a very long stack trace, it is a complete stack trace.
Listing 2. Default thread overflow output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at SimpleDump.main(SimpleDump.java:3)
As with many things on the Java platform, you can make changes if you don't like the default behavior. In previous versions of the Java platform Tiger version, the default behavior of all threads could not be overridden, but a new threadgroup could be created and the default behavior of any thread created within that group could be changed. You can override the Uncaughtexception (Thread T, Throwable e) method to customize the behavior. Then, when an unexpected run-time exception occurs, any threads created within that thread group will acquire new behavior. However, it is best to fix the underlying problem, and I will provide a simple example of the steps necessary to change the default behavior. Listing 3 shows an adjusted test program that puts execution code into a new thread:
Listing 3. Adjusted line Cheng Show example
public class WindowDump {
public static void main(String args[]) throws Exception {
ThreadGroup group = new LoggingThreadGroup("Logger");
new Thread(group, "myThread") {
public void run() {
System.out.println(1 / 0);
}
}.start();
}
}