java 线程 捕获异常 来自:thinking in java 4 目录20.2.13
package org.rui.thread.concurrent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 捕获异常 * * 下面的任务总是会抛出一个异常,该异常会传播到其run方法的外部, * 并且main展示了当你运行它时,所发生的事情 * @author lenovo * */ public class ExceptionThread implements Runnable { @Override public void run() { throw new RuntimeException(); } public static void main(String[] args) { /*ExecutorService exec=Executors.newCachedThreadPool(); exec.execute(new ExceptionThread()); */ try { ExecutorService exec=Executors.newCachedThreadPool(); exec.execute(new ExceptionThread()); } catch (Exception e) { System.out.println("eeeeeeeeeeeeeeee 该语句将不执行!"); } } } /**output: 以上输出结果一样: Exception in thread "pool-1-thread-1" java.lang.RuntimeException at org.rui.thread.concurrent.ExceptionThread.run(ExceptionThread.java:15) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) */
package org.rui.thread.concurrent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; /** * 捕获异常 * * 为了解决这个问题,我们要修改executor产生线程的方式。thread.UncaughtExceptionHandler是javaSE5中的新接口, * 它允许你在每个Thread对象上都附着一个异常处理器...... * * @author lenovo * */ class ExceptionThread2 implements Runnable { @Override public void run() { Thread t=Thread.currentThread(); System.out.println("run by : "+t); System.out.println(t.getUncaughtExceptionHandler()); throw new RuntimeException(); } } //////////////////无知的Exception class MyUncaughtExecptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("caught "+e); } } //////////////// class handlerThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { System.out.println("创建新的线程"); Thread t=new Thread(r); t.setUncaughtExceptionHandler(new MyUncaughtExecptionHandler()); System.out.println("eh= "+t.getUncaughtExceptionHandler()); return t; } } public class CaptureUncaughtExecption { public static void main(String[] args) { ExecutorService exec=Executors.newCachedThreadPool(new handlerThreadFactory() ); exec.execute(new ExceptionThread2()); } } /** output: 创建新的线程 eh= [email protected] run by : Thread[Thread-0,5,main] [email protected] 创建新的线程 eh= [email protected] caught java.lang.RuntimeException */
package org.rui.thread.concurrent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 这个处理器只有在不存在线程专有的末捕获异常处理器的情况下才会被调用。 * 系统会检查线程专有版 本,如果没有发现,则检查线程组是否有其专有的uncaughtException()方法, * 如果也没有,再调用defaultUncaughtExceptionHandler * @author lenovo * */ public class SettingDefaultHandler { public static void main(String[] args) { Thread.setDefaultUncaughtExceptionHandler( new MyUncaughtExecptionHandler() ); ExecutorService exec=Executors.newCachedThreadPool(); exec.execute(new ExceptionThread()); } } /** * output: caught java.lang.RuntimeException */
java 线程 捕获异常
时间: 2024-10-05 12:33:44