鉴于这种代码:为什么我的代码似乎绕过了这个异常?
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}
输出包括:
A C D E and F
我疑惑为什么飞机包括F,因为在等待()被调用的码外的IllegalMonitorStateException
抛出。为什么F的打印语句达到了?我相信线程堆栈会随之而来,但是程序应该将控制权交给主堆栈。
这是正确的吗?
+1绝对正确,如果你想捕捉异常时打印到日志,请确保你用throw抛出它们;而不是扔e;这将抛出一个新的例外。 – ajs410 2010-04-16 15:04:23