【发布时间】:2011-09-26 15:13:12
【问题描述】:
我有 Java 主类,在类中,我启动了一个新线程,在主类中,它一直等到线程死亡。在某个时刻,我从线程中抛出了运行时异常,但我无法在主类中捕获从线程中抛出的异常。
代码如下:
public class Test extends Thread
{
public static void main(String[] args) throws InterruptedException
{
Test t = new Test();
try
{
t.start();
t.join();
}
catch(RuntimeException e)
{
System.out.println("** RuntimeException from main");
}
System.out.println("Main stoped");
}
@Override
public void run()
{
try
{
while(true)
{
System.out.println("** Started");
sleep(2000);
throw new RuntimeException("exception from thread");
}
}
catch (RuntimeException e)
{
System.out.println("** RuntimeException from thread");
throw e;
}
catch (InterruptedException e)
{
}
}
}
有人知道为什么吗?
【问题讨论】:
标签: java multithreading