gpt4 book ai didi

JavaFX 默默地吞下拖动监听器中引发的异常

转载 作者:行者123 更新时间:2023-11-30 06:47:16 25 4
gpt4 key购买 nike

在 JavaFX 中,异常似乎被默默地吞没在监听器上的拖动中。我已经搜索过,但在文档中找不到任何提及此内容的内容。

我在下面重新创建了这个...

是否有办法防止这种情况并公开异常?

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();
}

}

根据 James_D 的反馈和进一步调查,我们发现这因平台而异

  • Ubuntu Linux。 1.8.0_40 => 发现异常
  • Mac OS X.1.8.0_40 => 出现异常
  • Windows 7.1.8.0_40 => 无异常(exception)
  • Windows 7. 1.8.0_121 => 无异常(exception)(撰写本文时为最新 JDK)

最佳答案

看来 native 方法 WinDnDClipboard.push(Object[], int) - 由 GlassClipboard.cpp 支持默默地吞掉异常。在调试器中可以看到对 Throwable.getMessage() 的回调,但没有异常打印到控制台。

此文件的 Java 9 版本 ( http://hg.openjdk.java.net/openjfx/9-dev/rt/file/1a3f128518cd/modules/javafx.graphics/src/main/native-glass/win/GlassClipboard.cpp ) 对 Utils.cpp 中定义的 CheckAndClearException(env); 进行了额外的调用,但这与看起来无关的 RT-35400 不符。这尚未向后移植到 Java 8。

关于JavaFX 默默地吞下拖动监听器中引发的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43488321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com