JavaFX silently swallowing exception raised in dra

2019-02-20 13:52发布

问题:

It appears that exceptions are silently swallowed in drag over listeners in JavaFX. I've searched and can't find any mention of this in the documentation.

I've recreated this below...

Is there anyway to prevent this and expose the exceptions?

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button source = new Button("source");
        Button destination = new Button("destination");
        HBox box = new HBox(source, destination);

        source.setOnDragDetected(event -> {
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);
            ClipboardContent content = new ClipboardContent();
            content.putString("foo");
            db.setContent(content);
            event.consume();
        });
        destination.setOnDragOver(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                event.acceptTransferModes(TransferMode.LINK);
                String nullReference = null; 
                nullReference.toCharArray(); // cause an exception
                event.consume();
            }
        });
        primaryStage.setScene(new Scene(box));
        primaryStage.show();
    }

}

Following feedback from James_D and further investigation, we have found this varies by platform

  • Ubuntu Linux. 1.8.0_40 => exceptions seen
  • Mac OS X. 1.8.0_40 => exceptions seen
  • Windows 7. 1.8.0_40 => No exceptions
  • Windows 7. 1.8.0_121 => No exceptions (latest JDK at time of writing)

回答1:

It appears that native method WinDnDClipboard.push(Object[], int) - backed by GlassClipboard.cpp swallows the exception silently. A call back to Throwable.getMessage() can be seen in the debugger but no exception is printed to the console.

The Java 9 version of this file (http://hg.openjdk.java.net/openjfx/9-dev/rt/file/1a3f128518cd/modules/javafx.graphics/src/main/native-glass/win/GlassClipboard.cpp) has an additional call to CheckAndClearException(env); as defined in Utils.cpp, however this is against RT-35400 which appears unrelated. This has not been backported to Java 8.