What to do with exceptions thrown by SwingUtilities.invokeAndWait

SwingUtilities.invokeAndWait() throws a InterruptedException and a InvocationTargetException how should I handle them?

  public static void invokeAndWait(Runnable doRun) throws InterruptedException, InvocationTargetException 

I want to use a method to display a dialog box and wait for the user to say yes or no. As far as I can tell, InvocationTargetException means there is a RuntimeException , and I can relate to it that way. However, what I really would like for an InterruptedException is to ignore it and continue the flow until the user gives an answer.

+4
source share
2 answers

InterruptedException is explained in this article .

InvocationTargetException that is thrown if the Runnables method throws an exception.

Perhaps launching this example clarifies the situation:

  try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { System.out.println("1"); if (true) { throw new RuntimeException("runtime exception"); } System.out.println("2"); } }); } catch (InterruptedException e) { e.printStackTrace(System.out); } catch (InvocationTargetException e) { e.printStackTrace(System.out); } 
+2
source

Throwing an exception when displaying a dialog box would be extremely rare. You can ignore it or try displaying the dialog again by invokingAndWait again in the new Runnable.

If the dialog is invoked as standalone (i.e. not as part of the main GUI), the use of invokeAndWait is fine. However, if you display a dialog from an EDT, you may not use invokeAndWait. Read more here: Concurrency in Swing

+1
source

Source: https://habr.com/ru/post/1301548/


All Articles