Community
Participate
Working Groups
The documentation of syncExec says that an SWTException is thrown if an exception occurs while executing the Runnable. This does not happen if you invoke syncExec inside a Job. For the following snippet: Actual result: Output is "done, no exception" Expected result: Output is "caught: class org.eclipse.swt.SWTException" new org.eclipse.core.runtime.jobs.Job("job") { protected org.eclipse.core.runtime.IStatus run(org.eclipse.core.runtime.IProgressMonitor monitor) { try { display.syncExec(new Runnable() { public void run() { throw new RuntimeException(); } }); System.out.println("done, no exception"); } catch (Throwable anyException) { System.out.println("caught: " + anyException.getClass()); } return org.eclipse.core.runtime.Status.OK_STATUS; } }.schedule();
The SWTException is thrown, but it's handled by the workbench's runEventLoop function before the syncExec returns.
(In reply to comment #1) > The SWTException is thrown, but it's handled by the workbench's runEventLoop > function before the syncExec returns. We have a org.eclipse.jface.window.Window.IExceptionHandler doing a handler.handleException(t); So this handler is called before Display#syncExec(*) has a change to propogate the exception back? Or syncExec cannot do that at all? PW
The workbench handler gets called before the syncExec returns. A syncExec call will wake the Display and wait until the runnable has executed on the main thread before returning.
If I take a snippet and make the dispatch loop look like ours, it works: while (!shell.isDisposed()) { try { if (!display.readAndDispatch()) display.sleep(); } catch (Throwable e) { System.err.println("dispatch: " + e); //$NON-NLS-1$ } } a thread runnable: public void run() { try { System.err.println("go"); //$NON-NLS-1$ display.syncExec(new Throw()); System.err.println("no"); //$NON-NLS-1$ } catch (Throwable e) { System.err.println("syncExec: " + e); //$NON-NLS-1$ } } I get: dispatch: org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException: There is no spoon) syncExec: org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException: There is no spoon)
Created attachment 132801 [details] Example Project Run this example project and click the new tool bar item. Output looks like: done, no exception !ENTRY org.eclipse.ui 4 0 2009-04-22 12:00:44.296 !MESSAGE Unhandled event loop exception !STACK 0 org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException) at org.eclipse.swt.SWT.error(SWT.java:3864) at org.eclipse.swt.SWT.error(SWT.java:3779) at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:137) at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3855) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3476) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2401) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2365) at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2217) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514) at org.eclipse.equinox.launcher.Main.run(Main.java:1287) at org.eclipse.equinox.launcher.Main.main(Main.java:1263) Caused by: java.lang.RuntimeException at forpaul.handlers.SampleHandler$1$1.run(SampleHandler.java:33) at org.eclipse.ui.internal.UILockListener.doPendingWork(UILockListener.java:155) at org.eclipse.ui.internal.UISynchronizer$3.run(UISynchronizer.java:158) at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35) at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134) ... 23 more
John, it is some interaction with the Job framework. If I modify Kevin's handler to both schedule the job and start the thread (with the same syncExec try/catch block in it) the job sees no exception and the thread gets the exception. PW
Off to Jobs PW
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.