Swingworker do in background method does not caught and print exceptions?
David Holmes
David.Holmes at oracle.com
Mon Sep 27 22:26:38 UTC 2010
Paulo,
Paulo Levi said the following on 09/28/10 04:23:
> In in linux 64 bits.
> java version "1.6.0_20"
> OpenJDK Runtime Environment (IcedTea6 1.9) (6b20-1.9-0ubuntu1)
> OpenJDK 64-Bit Server VM (build 17.0-b16, mixed mode)
> from ubuntu repositories.
>
> A very simple swingworker started from the EDT:
>
> new SwingWorker<Object, Object>() {
>
> @Override
> protected Object doInBackground() throws Exception {
> System.out.println("SW Ran");
> throw new NullPointerException();
> }
> }.run();
> Only prints SW Ran.
> It's surprising this was not noticed.
SwingWorker is a RunnableFuture. The exception will be caught and stored
for retrieval via the Future methods. Trying add a get() to the above
and it will throw the ExecutionException:
Exception in thread "main" java.util.concurrent.ExecutionException:
java.lang.NullPointerException
at
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at javax.swing.SwingWorker.get(SwingWorker.java:601)
at SW.main(SW.java:16)
Caused by: java.lang.NullPointerException
at SW$1.doInBackground(SW.java:11)
at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at javax.swing.SwingWorker.run(SwingWorker.java:335)
at SW.main(SW.java:14)
David Holmes
More information about the core-libs-dev
mailing list