gpt4 book ai didi

java - 我应该如何处理 SwingUtilities.invokeAndWait 抛出的异常

转载 作者:搜寻专家 更新时间:2023-11-01 02:55:36 29 4
gpt4 key购买 nike

SwingUtilities.invokeAndWait() 抛出一个 InterruptedException 和一个 InvocationTargetException 我应该如何处理这些?

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

我想使用该方法显示一个对话框并等待用户说是或否。据我所知,InvocationTargetException 意味着有一个 RuntimeException,我可以这样对待它。但是,对于 InterruptedException,我真正想要的是忽略它并让线程继续运行,直到用户给出答案。

最佳答案

InterruptedException 在此 article 中进行了解释.

InvocationTargetException,当Runnables run方法抛出异常时抛出。

也许运行这个例子可以澄清事情:

        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);
}

关于java - 我应该如何处理 SwingUtilities.invokeAndWait 抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2279389/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com