In the Java language, you divide exceptions into two categories:
inspected exception: This type of exception must be explicitly thrown in the throws clause or caught within the method. For example, a IOException exception or ClassNotFoundException exception.
non-inspected exceptions: This type of exception does not need to be explicitly thrown or caught. For example, a NumberFormatException exception.
When an exception is thrown in the thread object's run () method, we must capture and process it, because the run () method cannot throw an exception. When a non-inspected exception is thrown in the thread object's run () method, the default behavior is to print out the stack trace information on the console and then exit the program.
Fortunately, Java provides us with a mechanism specifically for handling non-inspected exceptions thrown by thread objects to avoid program exits.
In this section, we use examples to illustrate this mechanism.
Know it
Follow the steps shown below to implement our example.
1. First, we need to implement a class that handles non-inspected exceptions. This class must implement the Uncaughtexceptionhandler class to implement the Uncaughtexception () method declared in the interface. In this case, the class named Exceptionhandler,uncaughtexception () method prints out the exception and the thread information that threw the exception. The code is as follows:
Copy Code code as follows:
public class Exceptionhandler implements-Thread.uncaughtexceptionhandler {
@Override
public void uncaughtexception (Thread t, Throwable e) {
&N bsp; System.out.printf ("An exception has been captured\\n");
System.out.printf ("Thread:%s\n", T.getid ());
System.out.printf ("Exception:%s:%s\n", E.getclass (). GetName (),
e.getmessage ());
System.out.printf ("Stack Trace: \ n");
E.printstacktrace (System.out);
System.out.printf ("Thread Status:%s\n", t.getstate ());
}
}
2. Implement a class that can throw a non-inspected exception, called a task, implement the Runnable interface, implement the Run () method, and deliberately encode a section of code that produces a non-inspected exception, such as converting a string to a number. The code is as follows:
Copy Code code as follows:
public class Task implements Runnable {
@Override
public void Run () {
int numero = integer.parseint ("diguage.com");
}
}
3. Create the main class of the program, main class, and then implement the main () method. The code is as follows:
Copy Code code as follows:
public class Main {
public static void Main (string[] args) {
4. Create a Task object and create a thread object to execute it. Use the Setuncaughtexceptionhandler () method to set up a processing class for non-inspected exceptions. Then, start the thread. The code is as follows:
Copy Code code as follows:
Task task = new Task ();
Thread thread = new thread (Task);
Thread.setuncaughtexceptionhandler (New Exceptionhandler ());
Thread.Start ();
5. Run the sample to see the results.
Know the reason why
The results of exception execution can be seen from the following output fragment. The exception is thrown, and then the class is caught and the exception information is printed to the console.
Copy Code code as follows:
An exception has been captured
Thread:9
Exception:java.lang.NumberFormatException:For input string: "Diguage.com"
Stack Trace:
Java.lang.NumberFormatException:For input string: "Diguage.com"
At Java.lang.NumberFormatException.forInputString (numberformatexception.java:65)
At Java.lang.Integer.parseInt (integer.java:492)
At Java.lang.Integer.parseInt (integer.java:527)
At Com.diguage.books.concurrencycookbook.chapter1.recipe8.Task.run (task.java:13)
At Java.lang.Thread.run (thread.java:722)
Thread status:runnable
Process finished with exit code 0
When a thread throws an exception, and the exception, especially if it is not inspected, is not captured, the Java virtual Opportunity checks whether the non-inspected exception-handling class is set by the appropriate method, and calls the Uncaughtexception () method if it is already set. and pass the threads and exceptions as parameters to the method.
If you do not set up a processing class, the Java virtual machine prints the stack trace information in the console and then exits the program.
Endless
The thread class also has a method associated with non-inspected exception handling. This is the static method Setdefaultuncaughtexceptionhandler (), which sets the processing class for non-inspected exceptions for all threads in the program.
When an unhandled exception is thrown in a thread, the Java virtual opportunity looks for an exception-handling class from three places:
First, find the exception handling class from the thread object, which is what we learned in this section. If it does not exist, the exception handling class is looked up from the thread group (threadgroup) where the thread is located. About this part of the content, will be specifically explained later. If it still does not exist, find the program default exception handling class just mentioned above.
If none of the above mentioned exception handling exists, the Java Virtual machine prints the stack trace information for the exception to the console, and then exits the program.
Copycat
This article is from the "Java 7 Concurrency Cookbook" (D-Gua to "Java7 concurrent Sample Set") translation, only as learning materials used. No authorization shall be applied to any commercial act.
Small has become
Complete code for the Exceptionhandler class
Copy Code code as follows:
Package Com.diguage.books.concurrencycookbook.chapter1.recipe8;
/**
* Non-inspected exception handling class
* Date:2013-09-22
* Time:23:11
*/
public class Exceptionhandler implements Thread.uncaughtexceptionhandler {
@Override
public void Uncaughtexception (Thread t, Throwable e) {
System.out.printf ("An exception has been captured\n");
System.out.printf ("Thread:%s\n", T.getid ());
System.out.printf ("Exception:%s:%s\n", E.getclass (). GetName (),
E.getmessage ());
System.out.printf ("Stack Trace: \ n");
E.printstacktrace (System.out);
System.out.printf ("Thread Status:%s\n", t.getstate ());
}
}
Complete code for the task class
Copy Code code as follows:
Package Com.diguage.books.concurrencycookbook.chapter1.recipe8;
/**
* Exception Generation Class
* Date:2013-09-22
* TIME:23:18
*/
public class Task implements Runnable {
@Override
public void Run () {
int numero = integer.parseint ("diguage.com");
}
}
The complete code for the main class
Copy Code code as follows:
Package Com.diguage.books.concurrencycookbook.chapter1.recipe8;
/**
* Main class for sample
* Date:2013-09-22
* TIME:23:20
*/
public class Main {
public static void Main (string[] args) {
Task task = new Task ();
Thread thread = new thread (Task);
Thread.setuncaughtexceptionhandler (New Exceptionhandler ());
Thread.Start ();
}
}