我在执行软件时遇到以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Delay must be to 0 to 60,000ms
at java.awt.Robot.checkDelayArgument(Robot.java:544)
at java.awt.Robot.delay(Robot.java:534)
at com.company.Main.main(Main.java:10)
令我惊讶的是,有一个睡眠时间限制,并且标准库异常消息有错误的语法/拼写错误(to 0 to?)。检查该方法的源代码后delay(),我注意到它限制了等待时间,如异常所述:
/**
* Sleeps for the specified time.
* To catch any <code>InterruptedException</code>s that occur,
* <code>Thread.sleep()</code> may be used instead.
* @param ms time to sleep in milliseconds
* @throws IllegalArgumentException if <code>ms</code> is not between 0 and 60,000 milliseconds inclusive
* @see java.lang.Thread#sleep
*/
public synchronized void delay(int ms) {
checkDelayArgument(ms);
try {
Thread.sleep(ms);
} catch(InterruptedException ite) {
ite.printStackTrace();
}
}
private static final int MAX_DELAY = 60000;
private void checkDelayArgument(int ms) {
if (ms < 0 || ms > MAX_DELAY) {
throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
}
}
为什么要这样做?这似乎是糟糕的 API 设计。InterruptedException除了为你捕获多余的检查异常并同步调用之外,它还有什么目的?
萧十郎
相关分类