/** * Demonstrates difference of SwingWorker and Thread when an exception occurs. In SwingWorker, if you don't catch the exception in done(), it gets lost. */ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { new Thread(new Runnable() { @Override public void run() { System.out.println("Thread.run"); throw new IllegalArgumentException(); } }).start(); new SwingWorker() { @Override protected Object doInBackground() throws Exception { System.out.println("SwingWorker.doInBackground"); if (true) { throw new IllegalArgumentException(); } return null; } @Override protected void done() { try { get(); } catch (InterruptedException e) { System.out.println("Caught exception: " + e); } catch (ExecutionException e) { System.out.println("Caught exception: " + e); } } }.execute(); }
Output:
For more information see How should I handle exceptions when using SwingWorker?