这是我的程序,它生成一个InterruptedException。
timepass.java
public class timePass {
private static void book() {
System.out.print("book");
}
public static void main(String args[]){
Thread.sleep(1);
book();
}
}我想知道抛出异常的原因。
发布于 2016-03-09 06:26:40
将代码放入try catch块中
try
{
Thread.sleep(1);
book();
}
catch(InterruptedException e)
{
System.out.println("Error message");
}发布于 2016-03-09 06:28:50
这是因为Thread.sleep可能会抛出异常,所以您只需捕获它或将其赋予更高的级别。
public static void main(String args[]) throws Exception {
Thread.sleep(1);
book();
}发布于 2016-03-09 06:28:13
使用线程睡眠时,需要尝试捕获(InterruptedException e)
https://stackoverflow.com/questions/35884459
复制相似问题