Java – even with wrapper classes, swingworker exceptions are lost
I have been trying to solve the usability problem of swingworker. It will throw any exception in the background task, such as on this so thread This thread gives a good description of the problem, but does not discuss restoring the original exception
The applet I have delivered needs to propagate exceptions upward But I haven't caught it yet I am trying to solve this problem by using the simpleswingworker wrapper class in this blog entry This is a fairly small class, but I will reprint it at the end for reference only
The calling code looks like
try { // lots of code here to prepare data,finishing with SpecialDataHelper helper = new SpecialDataHelper(...stuff...); helper.execute(); // this will call get+done on the actual worker } catch (Throwable e) { // used "Throwable" here in desperation to try and get // anything at all to match,including unchecked exceptions // // no luck,this code is never ever used :-( }
packing:
class SpecialDataHelper extends SimpleSwingWorker { public SpecialDataHelper (SpecialData sd) { this.stuff = etc etc etc; } public Void doInBackground() throws Exception { OurCodeThatThrowsACheckedException(this.stuff); return null; } protected void done() { // called only when successful // never reached if there's an error } }
The function of simpleswingworker is that the do() / get() method of the actual swingworker is called automatically This theoretically refutes any exceptions that occur in the background In practice, nothing is caught, and I don't even know why
The simpleswingworker class is used for reference. For brevity, there are no messages:
import java.util.concurrent.ExecutionException; import javax.swing.SwingWorker; /** * A drop-in replacement for SwingWorker<Void,Void> but will not silently * swallow exceptions during background execution. * * Taken from http://jonathangiles.net/blog/?p=341 with thanks. */ public abstract class SimpleSwingWorker { private final SwingWorker<Void,Void> worker = new SwingWorker<Void,Void>() { @Override protected Void doInBackground() throws Exception { SimpleSwingWorker.this.doInBackground(); return null; } @Override protected void done() { // Exceptions are lost unless get() is called on the // originating thread. We do so here. try { get(); } catch (final InterruptedException ex) { throw new RuntimeException(ex); } catch (final ExecutionException ex) { throw new RuntimeException(ex.getCause()); } SimpleSwingWorker.this.done(); } }; public SimpleSwingWorker() {} protected abstract Void doInBackground() throws Exception; protected abstract void done(); public void execute() { worker.execute(); } }
Solution
Forget your packaging, it's eating abnormally, and swingworker doesn't The following is how to use swingworker and how to handle specific exceptions thrown from background tasks:
class MeaningOfLifeFinder extends SwingWorker<String,Object> { @Override public String doInBackground() throws SomeException { return findTheMeaningOfLife(); } @Override protected void done() { // called in the EDT. You can update the GUI here,show error dialogs,etc. try { String meaningOfLife = get(); // this line can throw InterruptedException or ExecutionException label.setText(meaningOfLife); } catch (ExecutionException e) { Throwable cause = e.getCause(); // if SomeException was thrown by the background task,it's wrapped into the ExecutionException if (cause instanceof SomeException) { // TODO handle SomeException as you want to } else { // the wrapped throwable is a runtime exception or an error // TODO handle any other exception as you want to } } catch (InterruptedException ie) { // TODO handle the case where the background task was interrupted as you want to } } }