菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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

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

posted on 2017-05-14 17:01  好吧,就是菜菜  阅读(178)  评论(0编辑  收藏  举报