gpt4 book ai didi

java - IllegalStateException 未捕获

转载 作者:行者123 更新时间:2023-11-30 04:15:09 26 4
gpt4 key购买 nike

我想在 Scala 中捕获 IllegalStateException。以下代码

val scene = new SceneDock

如果之前未启动 JavaFX 应用程序,则抛出 IllegalStateException。该构造函数来自 JemmyFX 框架。

所以我想通过以下方式捕获这个异常:

try{
val scene = new SceneDock
"running"}
catch{
case e: IllegalStateException => { "stopped" } }

但是这不起作用。输出始终是 IllegalStateException:工具包未初始化,并且它会重复,直到我终止应用程序。已经尝试过捕获异常,并用Java尝试过,但总是相同的结果。

堆栈跟踪(它会重复,直到我终止应用程序):

java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:155)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:150)
at javafx.application.Platform.runLater(Platform.java:52)
at org.jemmy.fx.QueueExecutor.executeQueue(QueueExecutor.java:98)
at org.jemmy.action.AbstractExecutor.execute(AbstractExecutor.java:100)
at org.jemmy.fx.SceneList.getControls(SceneList.java:58)
at org.jemmy.lookup.PlainLookup.getChildren(PlainLookup.java:76)
at org.jemmy.lookup.AbstractLookup.refresh(AbstractLookup.java:172)
at org.jemmy.lookup.AbstractLookup$1.reached(AbstractLookup.java:141)
at org.jemmy.lookup.AbstractLookup$1.reached(AbstractLookup.java:137)
at org.jemmy.timing.Waiter.waitState(Waiter.java:78)
at org.jemmy.timing.Waiter.ensureState(Waiter.java:119)
at org.jemmy.lookup.AbstractLookup.wait(AbstractLookup.java:137)
at org.jemmy.lookup.AbstractLookup.get(AbstractLookup.java:250)
at org.jemmy.lookup.AbstractLookup.wrap(AbstractLookup.java:210)
at org.jemmy.dock.Dock.lookup(Dock.java:75)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:20)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:32)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:60)
at org.djemmysl.main.DjemmySL$class.applicationStatus(DjemmySL.scala:25)
at org.djemmysl.main.Main$.applicationStatus(Main.scala:3)
at org.djemmysl.main.Main$.main(Main.scala:6)
at org.djemmysl.main.Main.main(Main.scala)

java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:155)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:150)
at javafx.application.Platform.runLater(Platform.java:52)
at org.jemmy.fx.QueueExecutor.executeQueue(QueueExecutor.java:98)
at org.jemmy.action.AbstractExecutor.execute(AbstractExecutor.java:100)
at org.jemmy.fx.SceneList.getControls(SceneList.java:58)
at org.jemmy.lookup.PlainLookup.getChildren(PlainLookup.java:76)
at org.jemmy.lookup.AbstractLookup.refresh(AbstractLookup.java:172)
at org.jemmy.lookup.AbstractLookup$1.reached(AbstractLookup.java:141)
at org.jemmy.lookup.AbstractLookup$1.reached(AbstractLookup.java:137)
at org.jemmy.timing.Waiter.waitState(Waiter.java:78)
at org.jemmy.timing.Waiter.ensureState(Waiter.java:119)
at org.jemmy.lookup.AbstractLookup.wait(AbstractLookup.java:137)
at org.jemmy.lookup.AbstractLookup.get(AbstractLookup.java:250)
at org.jemmy.lookup.AbstractLookup.wrap(AbstractLookup.java:210)
at org.jemmy.dock.Dock.lookup(Dock.java:75)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:20)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:32)
at org.jemmy.fx.SceneDock.<init>(SceneDock.java:60)
at org.djemmysl.main.DjemmySL$class.applicationStatus(DjemmySL.scala:25)
at org.djemmysl.main.Main$.applicationStatus(Main.scala:3)
at org.djemmysl.main.Main$.main(Main.scala:6)
at org.djemmysl.main.Main.main(Main.scala)

最佳答案

实例化SceneDock等待一段时间(我猜默认情况下是10秒),直到启动jemmy fx应用程序。在此等待期间,它会定期(默认情况下每 100 毫秒)查找是否通过在 javafx thread 中执行任务(使用 Platform.runLater(Runnable))打开了窗口。 。

在没有启动应用程序时调用Platform.runLater(Runnable),会抛出java.lang.IllegalStateException,捕获并打印到错误输出:

java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:153)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:148)
...

如果一段时间后没有完成任何申请,您应该得到一个org.jemmy.TimeoutExpiredException:

org.jemmy.TimeoutExpiredException: State 'Waiting for 1 javafx.scene.Scene controls to be found adhering to org.jemmy.lookup.Any@65e7c41f' has not been reached in 10000 milliseconds
at org.jemmy.timing.Waiter.ensureState(Waiter.java:121)
at org.jemmy.lookup.AbstractLookup.wait(AbstractLookup.java:138)
...

我通过以下代码得到此行为:

import org.jemmy.TimeoutExpiredException;
import org.jemmy.fx.SceneDock;
import org.junit.Test;

public class TestX {

@Test
public void notInitializedToolkit() {
try {
new SceneDock();

} catch (TimeoutExpiredException e) {
System.err.println("The application have not been launched");
}
}
}

关于java - IllegalStateException 未捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574113/

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