This article is a summary of the study of the article on the network, thank you for your selfless sharing.
1 , Java in a 2 type of exception:
Check for exceptions: These exceptions must be forced to capture them or in a method throws clause.
No exception checked: These exceptions do not have to be forced to capture them.
2 , in a thread object's Run () method throws a check exception, we must capture and process them. Because the run() method does not accept the throws clause. When a non-check exception is thrown, the default behavior is to write the stack trace in the console and exit the program.
Package Chapter;public class Main8 {/** * <p> * </p> * @author Zhangjunshuai * @date 2014-8-21 pm 2:14:52 * @pa Ram args */public static void main (string[] args) {Task task = new Task (); Thread thread = new thread (Task); Thread.setuncaughtexceptionhandler (new Exceptionhandler ()); Thread.Start ();}}
Package Chapter;public class Task implements runnable{@Overridepublic void Run () {int numero = integer.parseint ("TTT");}}
Package Chapter;import Java.lang.thread.uncaughtexceptionhandler;public class Exceptionhandler implements uncaughtexceptionhandler{@Overridepublic 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 traace"); E.printstacktrace (System.out); System.out.printf ("Thread status:%s\n", T.getstate ());}}
Operation Result: