如何捕获事件调度线程 (EDT) 异常?

2024-03-03

我正在使用一个名为MyExceptionHandler实现Thread.UncaughtExceptionHandler处理我的项目中的正常异常。

据我了解,这个类无法捕获 EDT 异常,所以我尝试在main()处理EDT异常的方法:

public static void main( final String[] args ) {
    Thread.setDefaultUncaughtExceptionHandler( new MyExceptionHandler() );  // Handle normal exceptions
    System.setProperty( "sun.awt.exception.handler",MyExceptionHandler.class.getName());  // Handle EDT exceptions
    SwingUtilities.invokeLater(new Runnable() {  // Execute some code in the EDT. 
        public void run() {
            JFrame myFrame = new JFrame();
             myFrame.setVisible( true );
        }
    });
}

但直到现在它还不起作用。例如,在初始化 JFrame 时,我从构造函数中的捆绑文件加载其标签,如下所示:

setTitle( bundle.getString( "MyJFrame.title" ) );

我删除了密钥MyJFrame.title从捆绑文件中测试异常处理程序,但它不起作用!异常通常会打印在日志中。

我在这里做错了什么吗?


EDT 异常处理程序不使用Thread.UncaughtExceptionHandler。相反,它调用具有以下签名的方法:

public void handle(Throwable thrown);

将其添加到MyExceptionHandler,它应该可以工作。

这方面的“文档”位于EventDispatchThread,这是一个包私有类java.awt。引用javadoc为handleException() there:

/**
 * Handles an exception thrown in the event-dispatch thread.
 *
 * <p> If the system property "sun.awt.exception.handler" is defined, then
 * when this method is invoked it will attempt to do the following:
 *
 * <ol>
 * <li> Load the class named by the value of that property, using the
 *      current thread's context class loader,
 * <li> Instantiate that class using its zero-argument constructor,
 * <li> Find the resulting handler object's <tt>public void handle</tt>
 *      method, which should take a single argument of type
 *      <tt>Throwable</tt>, and
 * <li> Invoke the handler's <tt>handle</tt> method, passing it the
 *      <tt>thrown</tt> argument that was passed to this method.
 * </ol>
 *
 * If any of the first three steps fail then this method will return
 * <tt>false</tt> and all following invocations of this method will return
 * <tt>false</tt> immediately.  An exception thrown by the handler object's
 * <tt>handle</tt> will be caught, and will cause this method to return
 * <tt>false</tt>.  If the handler's <tt>handle</tt> method is successfully
 * invoked, then this method will return <tt>true</tt>.  This method will
 * never throw any sort of exception.
 *
 * <p> <i>Note:</i> This method is a temporary hack to work around the
 * absence of a real API that provides the ability to replace the
 * event-dispatch thread.  The magic "sun.awt.exception.handler" property
 * <i>will be removed</i> in a future release.
 */

Sun 究竟是如何期望你找到这个的,我不知道。

下面是一个完整的示例,它捕获 EDT 内外的异常:

import javax.swing.SwingUtilities;

public class Test {
  public static class ExceptionHandler
                                   implements Thread.UncaughtExceptionHandler {

    public void handle(Throwable thrown) {
      // for EDT exceptions
      handleException(Thread.currentThread().getName(), thrown);
    }

    public void uncaughtException(Thread thread, Throwable thrown) {
      // for other uncaught exceptions
      handleException(thread.getName(), thrown);
    }

    protected void handleException(String tname, Throwable thrown) {
      System.err.println("Exception on " + tname);
      thrown.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
    System.setProperty("sun.awt.exception.handler",
                       ExceptionHandler.class.getName());

    // cause an exception on the EDT
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        ((Object) null).toString();        
      }
    });

    // cause an exception off the EDT
    ((Object) null).toString();
  }
}

应该可以做到这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何捕获事件调度线程 (EDT) 异常? 的相关文章

随机推荐

  • 将项目添加到组合框

    我有一个ComboBox控制 我将此控件绑定到 DataSet 表 这是代码 comboBox Items Add Select comboBox DataSource DataSet ColorTable comboBox Display
  • 打包和解包 64 位整数

    我有以下代码 packed pack i PHP INT MAX echo unpack i packed 1 结果我得到 1 我在用着PHP 5 4 6 1ubuntu1 1 cli built Nov 15 2012 01 18 34
  • 通过 PHP 使用 EPL 打印:存储图像

    背景信息 Mac OS X Lion 10 7 3 11D50b EPL http en wikipedia org wiki Eltron Programming Language http en wikipedia org wiki E
  • 向形状/线条添加渐变

    我正在尝试使用 VBA 将渐变添加到 Excel 中的线条形状 此功能可在Line Color下的部分Format Shape选项 尽管此功能存在于Format Shape选项 我无法在 VBA 中重现该功能 我的代码是 With Acti
  • 我可以通过 Lucene 在 Orchard 中搜索/索引自定义数据源吗?

    我目前正在开发一个网站 允许用户搜索自定义产品目录 我一直在寻找并希望利用 Orchard CMS 来帮助我开发这个网站 我目前已经经历了罗恩 彼得森的 YouTube 系列 http www youtube com watch v Iv7
  • 输入助手 valueBinding 已弃用 - 有什么替代方案?

    我有一些像这样的文本输入助手 input type text valueBinding name focus out focusOutName 我刚刚将 Ember 升级到 1 11 0 现在收到此弃用警告 弃用 您尝试通过将 valueB
  • 如何通过ajax将值传递给php变量

    这是我的 JavaScript 代码 function category row dataparam oper delete row row ajax type POST url multiupload php data dataparam
  • Helm Charts 中的秘密管理

    我正在尝试使用Helm charts在 Kubernetes 集群中安装应用程序 有人可以建议什么是更好的秘密管理解决方案吗 使用helm secrets是个好主意或者Hashicorp Vault Vault 在技术上非常棒 但它可能会成
  • 如何创建类似谷歌纵横的标记?

    在我的 HTML5 应用程序中 我使用 Google Map v3 并在地图上添加多个标记 放置新标记和更改图标很容易 但我希望能够构建像谷歌纬度中使用的标记一样的标记 这些标记设置有图标图像和漂亮的边框 关于如何做到这一点有什么想法吗 您
  • 使用 bootstrap 无法在 angularjs 中打开模式窗口

    这是我的 app js 文件 const app angular module CurseTransport ui router ui bootstrap ngMessages raceModule app config stateProv
  • Azure函数应用程序-在执行和间歇性运行的旧代码之间共享全局变量

    目前 我在 Azure 函数应用程序中面临两个问题 我已提供以下详细信息 1 全局变量内容在执行之间共享 我使用了并发字典 它是一个全局变量 私有的和静态的 该变量在队列触发器中使用 private static readonly Conc
  • iphone开发:验证来自https url的证书信息

    当用户使用网络浏览器 Safari Chrome等 连接到 https url 时 例如 https encrypted google com 则用户可以获得有关证书相关的信息到这样的 https url 也就是说 在连接到url http
  • 使用 HTML 表单和 PHP 更新 MySQL 数据库字段数据

    所以我试图使用 html 表单和一些 PHP 代码更新数据库字段 但我无法让它工作 它不会抛出任何错误 但不会更新该字段 我不确定它是否因为我也回显该字段网页 它似乎所做的就是打印失败消息 HTML
  • MSSQL - 将一个字段拆分为 3 个字段

    我有一个由 1 列组成的结果集 在本例中为 2 行 单列 ProductDescription 是一个 varchar 字段 其中包含 3 条信息 我没有设计它 我需要将这三条信息分成 3 个使用查询的附加字段 before Product
  • 在Bootstrap组件中单独加载

    我正在开发一个涉及许多开发人员的大型 Web 项目 我想精简 Bootstrap3 的包并仅保留我们正在使用的内容 基本上 这个想法是在页面加载到浏览器中时减少任何额外的开销 所以我可以通过两种方法来做到这一点 我也可以 a 从库中删除任何
  • 如何使用 lambda 表达式创建扩展方法

    目前我正在创建一个接受参数的扩展方法 使用下面的示例 如何使用 lambda 表达式对其进行转换 public static decimal ChangePercentage this IEnumerable
  • 根据前缀对目录中的文件进行分组

    我有一个包含图片的文件夹 文件夹 1 Files ABC 138923 ABC 3223 ABC 33489 ABC 3111 CBA 238923 CBA 1313 CBA 1313 DAC 38932 DAC 1111 DAC 1389
  • 使用 Microsoft Graph API 获取 SharePoint Online 团队网站

    我正在尝试访问组织的 SharePoint 团队网站 我使用 Microsoft Graph API 因为它是 Office 365 最完整的 API 我了解如何获取访问令牌以及如何使用它来发出请求 我知道它有效 因为我可以获得组列表 但是
  • 获取ejs模板中的url参数

    我试图根据 URL 参数创建一个 ejs 条件 例如 如果测试参数存在于 localhost 3000 page test 则显示一个 div 否则不显示它 我的 ejs 模板看起来像这样 div class row div div div
  • 如何捕获事件调度线程 (EDT) 异常?

    我正在使用一个名为MyExceptionHandler实现Thread UncaughtExceptionHandler处理我的项目中的正常异常 据我了解 这个类无法捕获 EDT 异常 所以我尝试在main 处理EDT异常的方法 publi