【发布时间】:2016-11-23 21:03:11
【问题描述】:
public static void main(String s[])
{
Thread t=Thread.currentThread();
t.setName("main");
try
{
for(int i=0;i<=5;i++)
{
System.out.println(i);
Thread.sleep(1000);//interrupted exception(System provides error on its own)
}
}
catch(InterruptedException e)
{
System.out.println("main thread interrupted");
}
}
`据我了解,当出现异常情况时,控件会转到 catch,实现它并离开代码。当我们使用 thread.sleep 并为 interruptedException 创建一个 catch 时,为什么它会继续运行?而不是退出。这是代码,当 for 循环第一次运行时,它会在遇到 thread.sleep 时打印“0”,因此会出现中断异常,它不应该去捕获并执行 S.O.P 并终止吗?
【问题讨论】:
-
嗯...您是否触发了导致
sleep终止的异常? -
如果你不告诉它,你为什么会认为它会终止?
标签: java interrupted-exception interruption