JDK-4452162 : Exception thrown inside the Robot's delay(...) method cannot be caught
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_98
  • CPU: x86
  • Submitted: 2001-04-29
  • Updated: 2018-10-08
  • Resolved: 2001-06-28
Related Reports
Relates :  
Description

Name: boT120536			Date: 04/29/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


The InterruptedException exception generated inside the java.awt.Robot class's
delay(...) method can not be handled by the calling application. The bug can be
found in the Java 2 SDK 1.3.1 RC1 on Windows NT 4.0, too. Here is the sample
code, which clearly demonstrates the bug:

/*
 * This code demonstrates the bug in the java.awt.Robot class related to the
 * delay(...) method. An interruption of the thread, waiting for the expiration
 * of the time period specified as an argument to the delay(...) method, could
 * cause an abrupt exit from the application. The exception generated inside
 * the Robot's delay(...) method can not be handled!
 */
 
import java.awt.*;

public class RobotTest {
    static Robot robot;
    
    static class DormantThread extends Thread {
        public void run() {
            try {
                // This call should behave exactly as Thread.sleep(5000), but
                // due to the bug in the Robot class, it does not.
                robot.delay(5000);
            } catch (Throwable ignored) {}
        }
    }
            
    public static void main(String args[]) {
        Thread t = new DormantThread();
        
        try {
            robot = new Robot();
        } catch (AWTException e) {
            System.exit(1);
        }

        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ingored) {}
        t.interrupt();
        
        System.exit(0);
    }
}

After running the program listed above, the following error message is shown in
the command window:

java.lang.InterruptedException: sleep interrupted
        at java.lang.Thread.sleep(Native Method)
        at java.awt.Robot.delay(Robot.java:356)
        at RobotTest$DormantThread.run(RobotTest.java:19)

By analyzing the sample code, it is obvious, that this message should never
appear in the command window.
(Review ID: 121165) 
======================================================================

Comments
I do not know why it was closed as Won't Fix. This bug was fixed by adding this part of the spec: /** * Sleeps for the specified time. + * To catch any {@code InterruptedException}s that occur, + * {@code Thread.sleep()} may be used instead. * Which actually does not solve the initial issue with inability to suppress the stack trace.
08-10-2018

WORK AROUND Name: boT120536 Date: 04/29/2001 The work around is to avoid using Robot.delay(...) method for delaying the playback of events. Instead, the Thread.sleep(...) method should be used. ======================================================================
11-06-2004

EVALUATION Changing Robot.delay() to throw an InterruptedException would break existing code. However, there is a reasonable workaround which can be mentioned in the JavaDoc. brent.christian@eng 2001-06-26 Added said documentation to 1.4. brent.christian@eng 2001-06-28
26-06-2001