Why doesn't my Java exception print stack trace when throwing inside SwingWorker?

Why does this happen when I wrap SwingWorker around this code, it no longer reports an exception?

import java.security.InvalidParameterException; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; public class Test { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new SwingWorker<Void, Void>() { @Override protected Void doInBackground() throws Exception { IntegerString s = new IntegerString("EIGHT"); return null; } }.execute(); } }); } } class IntegerString { public IntegerString(String s) { if (!isInteger(s)) { System.out.println("...throwing exception."); throw new InvalidParameterException("Thrown."); } // ... } static boolean isInteger(String str) { if (str == null) { return false; } int length = str.length(); if (length == 0) { return false; } int i = 0; if (str.charAt(0) == '-') { if (length == 1) { return false; } i = 1; } for (; i < length; i++) { char c = str.charAt(i); if (c <= '/' || c >= ':') { return false; } } return true; } } 
+6
source share
1 answer

You must call get() to get all the exceptions that occurred in doInBackground() . For example, you can do this in the done() method, that is:

 @Override protected void done() { try { get(); } catch (Exception e) { e.printStackTrace(); } } 
+8
source

All Articles