SwingUtilities.invokeAndWait()抛出一个InterruptedException和一个InvocationTargetException,我该如何处理它们?
public static void invokeAndWait(Runnable doRun) throws InterruptedException,
InvocationTargetException我想使用该方法显示一个对话框,并等待用户说是或否。据我所知,InvocationTargetException意味着存在一个RuntimeException,我可以这样对待它。但是,我真正希望InterruptedException忽略它,并让线程继续运行,直到用户给出答案。
发布于 2010-02-17 19:38:16
本article中对InterruptedException进行了说明。
如果Runnables run方法抛出异常,就会抛出InvocationTargetException。
也许运行这个例子可以澄清一些事情:
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);
}发布于 2010-02-17 17:20:35
在显示对话框时使用InterruptedException的情况非常少见。您可以忽略它,或者尝试通过在新的Runnable上再次调用invokeAndWait来再次显示对话框。
如果对话框是作为独立的(即不是主图形用户界面的一部分)调用的,那么使用invokeAndWait就可以了。但是,如果您在EDT中显示该对话框,则可能不希望使用invokeAndWait。更多详细信息请访问:Concurrency in Swing
https://stackoverflow.com/questions/2279389
复制相似问题