UncaughtExceptionTask task=new UncaughtExceptionTask();
Thread thread = new Thread(task);
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("处理run函数抛出的异常");
System.out.println(t+":"+e);
}
});
thread.start();
}
class UncaughtExceptionTask implements Runnable{
public void run(){
System.out.println(10/0);//throws uncheckerException: ArithmeticException
}
}
public class ExceptionTest { public static void main(String[] args) { int c=0; try { int a = 3; int b = 0; c=a/b; System.out.println(c); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("处理了异常: ArithmeticException"); } System.out.println(c); } }
原文:http://www.cnblogs.com/yangyunnb/p/6057874.html