【发布时间】:2018-07-07 11:34:30
【问题描述】:
在下面的代码中,写在匿名类实例化中的Thread.sleep(3000); 只能使用try-catch 块来处理。为什么throws InterruptedException 子句不允许传播异常?
public static void main(String[] args) throws InterruptedException {
Runnable task = new Runnable() {
public void run() {
// below line explicitly need to be handled using try-catch. throws keyword does not work here
Thread.sleep(3000);
}
};
}
【问题讨论】:
-
这与字段声明无关。尝试将
Runnable task = ...放入方法中:您将得到完全相同的错误。
标签: java exception-handling try-catch throws