为什么要设置打扰" sun.awt.exception.handler"属性? [英] Why bother with setting the "sun.awt.exception.handler" property?

查看:183
本文介绍了为什么要设置打扰" sun.awt.exception.handler"属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些code映入扔在事件指派线程异常:

Here's some code that catches an exception thrown on the Event Dispatch Thread:

package com.ndh.swingjunk;

import java.awt.EventQueue;

import javax.swing.JFrame;

public class EntryPoint {

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

        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new SomeWindow("foo").setVisible(true);
            }
        });
    }
}

class SomeWindow extends JFrame {
    public SomeWindow(String title) {
        this.setTitle(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        throw new RuntimeException("hello");
    }
}

我见过扔在事件指派线程异常没有得到通过的UncaughtExceptionHandler处理警告,但似乎并没有成为我的例子的情况;它的工作原理相同的注册行是否被注释掉或离开。是我搞砸的例子在某种程度上,或注册的异常处理程序 sun.awt.exception.handler 没有存在的必要?

推荐答案

EDT 类( java.awt.EventDispatchThread ,不寻找它在javadoc中,这个类的包私有的)自AWT的起源发生了很大变化。

The EDT class (java.awt.EventDispatchThread, don't look for it in the javadoc, this class is package private) has changed a lot since the origins of AWT.

JDK6 上,你可以看到,这个类现在可以正确处理异常的EDT里面存在的。异常处理是当前版本的有点复杂:

In JDK6, you can see that this class now can properly handle exceptions occuring inside the EDT. Exception handling is a bit complex in the current version:


  • 如果您已设置的
    sun.awt.exception.handler 属性,
    那么你的处理程序将被要求
    每个异常由开发者抛出
    code称为EDT内
    (用previous JDK兼容性
    版本保证)。

  • ,否则,任何异常会
    重新抛出,因此将停止当前EDT,和任何违约
    的UncaughtExceptionHandler
    能赶上它,因为你的代码段
    演示。

  • if you have set the sun.awt.exception.handler property, then your handler will be called for every exception thrown by developer's code called inside the EDT (compatibility with previous JDK versions is ensured).
  • otherwise, any exception will be rethrown, hence will stop the current EDT, and any default UncaughtExceptionHandler will be able to catch it, as your snippet demonstrates.

BUT (这是<强>很重要),如果你仔细观察一下EDT的code,你会看到,这种机制将无法工作如果在显示一个模式对话框中EDT 出现异常(我猜这是因为EDT和的EventQueue 管理​​是相当复杂的,我甚至会敢说的凌乱的:很多code看起来像在那里黑客)

BUT (and this is very important), if you carefully look at the code of EDT, you'll see that this mechanism won't work if the exception occurs in the EDT while a modal dialog is displayed (I guess this is because EDT and EventQueue management is quite complicated and I would even dare say "messy": a lot of code looks like hacks in there).

在这个确切的情况,异常情况将被记录到 System.err的,除非你已经设置了 sun.awt.exception.handler 属性。有一个默认的的UncaughtExceptionHandler 也无济于事。

In this exact situation, exceptions will be logged to System.err, except if you have set the sun.awt.exception.handler property. Having a default UncaughtExceptionHandler will not help.

所以,我拿到这个是,是,你还是应该用 sun.awt.exception.handler 打扰财产,除非你可以肯定地说,你的应用程序不使用模态对话框(不要忘记,的JOptionPane 对话也是模态)。

So my take on this is that, YES, you should still bother with sun.awt.exception.handler property, except if you can say for sure that your application doesn't use modal dialogs (don't forget that JOptionPane dialogs are also modal).

这篇关于为什么要设置打扰&QUOT; sun.awt.exception.handler&QUOT;属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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