/** * Dispatch an uncaught exception to the handler. This method is * intended to be called only by the JVM. */ private void dispatchUncaughtException(Throwable e) { getUncaughtExceptionHandler().uncaughtException(this, e); }
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.println("default uncaughtExceptionHandler catch a exception " + "from thread name= " + Thread.currentThread().getName() + " ," + " threadGourp= " + Thread.currentThread().getThreadGroup().getName() ); System.out.println(e.getMessage()); } }); ThreadGroup threadGroup = new ThreadGroup("fuyi"); Thread t1 = new Thread(threadGroup, new Runnable(){ public void run() { System.out.println("线程" + Thread.currentThread().getName() + "run....."); int i = 1/0; } }, "t1"); Thread t2 = new Thread(threadGroup, new Runnable(){ public void run() { System.out.println("线程" + Thread.currentThread().getName() + "run....."); int i = 1/0; } }, "t2"); t1.start(); t2.start(); try { Thread.sleep(2000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
//结果 线程t2run..... 线程t1run..... default uncaughtExceptionHandler catch a exception from thread name= t2 , threadGourp= fuyi / by zero default uncaughtExceptionHandler catch a exception from thread name= t1 , threadGourp= fuyi / by zero
if (g == null) { /* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter use the parent thread group. */ if (g == null) { g = parent.getThreadGroup(); } }