我在執(zhí)行軟件時(shí)遇到以下異常: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)令我驚訝的是,有一個(gè)睡眠時(shí)間限制,并且標(biāo)準(zhǔn)庫(kù)異常消息有錯(cuò)誤的語(yǔ)法/拼寫(xiě)錯(cuò)誤(to 0 to?)。檢查該方法的源代碼后delay(),我注意到它限制了等待時(shí)間,如異常所述:/** * 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 設(shè)計(jì)。InterruptedException除了為你捕獲多余的檢查異常并同步調(diào)用之外,它還有什么目的?
為什么 Robot.delay(int ms) 限制為 1 分鐘?
婷婷同學(xué)_
2023-08-09 17:17:30