为什么在 Java 中需要对以下行使用 try/catch 异常处理语句:
TimeUnit.MILLISECONDS.sleep(250);
我只想让我的代码在执行下一行之前休眠 250 毫秒。为什么需要 try-catch 语句?我不能预见任何例外。python 中的 sleep 函数不需要 try catch。
我在 IDE 中没有添加 try catch 的异常是:
Error:(124, 40) error: unreported exception InterruptedException; must be caught or declared to be thrown
当我按照下面的方法将它包围在 try catch 中时,它可以正常工作:
try {
TimeUnit.MILLISECONDS.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}