如果您检查代码Platform.runLater()
(见下文),您将看到异常被吞没(第 146 / 147 行),因此默认的未捕获异常处理程序将无法捕获它们 - 基于那段代码,我不'认为除了在可运行文件中包含 try/catch 块外,您别无选择。
请注意,此问题已报告(需要登录 - 注册是免费的)并且应该在 Lombard 中修复(= Java FX 8.0 将于明年与 Java 8 一起发布)。
您也可以创建一个实用方法并调用
Platform.runLater(getFxWrapper(yourRunnable));
public static Runnable getFxWrapper(final Runnable r) {
return new Runnable() {
@Override
public void run() {
try {
r.run();
} catch (Exception e) {
//here you probably want to log something
System.out.println("Found an exception");
}
}
};
}
代码Platform.runLater
:
120 private static void runLater(final Runnable r, boolean exiting) {
121 if (!initialized.get()) {
122 throw new IllegalStateException("Toolkit not initialized");
123 }
124
125 pendingRunnables.incrementAndGet();
126 waitForStart();
127
128 if (SystemProperties.isDebug()) {
129 Toolkit.getToolkit().pauseCurrentThread();
130 }
131
132 synchronized (runLaterLock) {
133 if (!exiting && toolkitExit.get()) {
134 // Don't schedule a runnable after we have exited the toolkit
135 pendingRunnables.decrementAndGet();
136 return;
137 }
138
139 Toolkit.getToolkit().defer(new Runnable() {
140 @Override public void run() {
141 try {
142 r.run();
143 pendingRunnables.decrementAndGet();
144 checkIdle();
145 } catch (Throwable t) {
146 System.err.println("Exception in runnable");
147 t.printStackTrace();
148 }
149 }
150 });
151 }
152 }