我想在Scala中捕获IllegalStateException。以下代码
val scene = new SceneDock
如果以前没有启动JavaFX-Application,则抛出IllegalStateException。这个构造函数来自JemmyFX框架。
所以我想通过以下方式捕获此异常:
try{
val scene = new SceneDock
"running"}
catch{
case e: IllegalStateException => { "stopped" } }
但它不起作用。输出始终是IllegalStateException:Toolkit未初始化,它会重复,直到我终止应用程序。已经尝试捕获Exception,并尝试使用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)
答案 0 :(得分:2)
实例化SceneDock
等待一点(默认情况下为10秒),直到jemmy fx
应用程序启动。
在此等待期间,如果通过执行javafx thread
中的任务(使用Platform.runLater(Runnable)
)打开了一个窗口,则会定期查找(默认情况下每100毫秒)。
在没有启动应用程序时调用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");
}
}
}