EDT 和运行时异常 [英] EDT and runtime exception

查看:32
本文介绍了EDT 和运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从 EDT 线程调用某个方法并出现未经检查的异常会发生什么?它是否支持 GUI 的响应能力或什么?谢谢

What would happen if certain method invoked from EDT thread thew unchecked exception? Does it hold up responsiveness of GUI or what? Thank you

推荐答案

默认情况下,如果未捕获到异常,堆栈跟踪将写入控制台输出.整个 GUI 不会变得无响应(但是,正如垃圾神所说,特定组件可能会保持不自然的状态),EDT 继续工作:抛出异常时EDT是否重新启动?

By default, if the exception is not caught, the stack trace is written to the console output. The GUI as a whole does not become unresponsive (but, as trashgod commented, the particular component can remain in an unnatural-looking state), the EDT continues to work: Does the EDT restart or not when an exception is thrown?

好的做法是设置一个未捕获的异常处理程序,因为您想知道是否出了问题.请注意(取决于 Java 版本)这对于 EDT 的功能可能与其他线程不同:

The good practice is to set up an uncaught exception handler, because you want to know if something went wrong. Note that (depending on the Java version) this might function differently for the EDT than for other threads:

我如何检测何时在 Java 中全局抛出异常?

请注意,许多 SO 帖子中提到的sun.awt.exception.handler"技巧不是必需的,并且在 Java 7 中不起作用.对于 Java 7,只需使用标准的 Thread.setDefaultUncaughtExceptionHandler.当然,如果您同时使用这两种机制来注册异常处理程序,则代码将适用于所有版本.

Note that the "sun.awt.exception.handler" trick, mentioned in many SO posts, is not necessary and does not work in Java 7. For Java 7 just use the standard Thread.setDefaultUncaughtExceptionHandler. Of course, if you use both mechanisms to register the exception handler, the code will work in all versions.

这篇关于EDT 和运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆