首页 > 代码库 > 线程异常测试

线程异常测试

import java.lang.Thread.UncaughtExceptionHandler;public class ThreadException{	public static void main(String[] args)	{		try		{			MyThread t1 = new MyThread(0);			t1.start();			MyThread t2 = new MyThread(1);			t2.start();		}		catch (Exception e)		{			System.out.println(e.getMessage());		}		while(true);	}}class MyThread extends Thread{	int i;	public MyThread(int i)	{		this.i = i;	}	public void run()	{		if (i == 0) throw new RuntimeException(i + "");		else			System.out.println("thead end");	}}

  结果

Exception in thread "Thread-0" java.lang.RuntimeException: 0	at MyThread.run(ThreadException.java:33)thead end

  异常被封闭在当前线程,不会抛到开启线程的方法中来

线程异常测试