【发布时间】:2014-12-29 11:09:20
【问题描述】:
要编译这段代码,我可以:
- 在 try/catch 块中调用
Thread.sleep(),或者 - 让
printAll()声明它可以抛出InterruptedException。
为什么我必须这样做?
class Test {
public static void main( String[] args ) {
printAll( args );
}
public static void printAll( String[] line ) {
System.out.println( lines[ i ] );
Thread.currentThread().sleep( 1000 ):
}
}
(来自Kathy Sierra's SCJP book的示例代码。)
我知道Thread.sleep()抛出的异常是受检异常,所以我要处理它,但是Thread.sleep()在什么情况下需要抛出这个异常呢?
【问题讨论】:
-
因为 Sleep 方法检查了异常...
-
感谢您的重播,我知道这个检查异常,但是为什么在什么情况下我们需要处理异常呢?
标签: java multithreading exception