【发布时间】:2013-06-07 11:20:13
【问题描述】:
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted, NOT PRINTED");
}
System.out.println ("This statement is printed");
在这段代码中,sleep 抛出了一个中断的异常,但这并没有在输出中打印 catch 语句。为什么?
【问题讨论】:
-
中断在哪里完成?我看这里没有扔东西。
-
在开头添加
Thread.currentThread().interrupt()。 -
sleep 方法应该抛出异常,它的原型有点像这样:static void sleep(long milliseconds) throws InterruptedException
-
@Mayank - 睡眠方法可能抛出异常,if睡眠线程被中断。它不保证这样做。
标签: java exception try-catch thread-sleep