如何检测何时在 Java 中全局抛出异常? [英] How can I detect when an Exception's been thrown globally in Java?

查看:29
本文介绍了如何检测何时在 Java 中全局抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测我的应用程序中何时抛出异常?

How can I detect when an Exception has been thrown anywhere in my application?

每当我的 Java 桌面应用程序中的任何地方抛出异常时,我都会尝试自动向自己发送一封电子邮件.我想这样我可以更积极主动.

I'm try to auto-magically send myself an email whenever an exception is thrown anywhere in my Java Desktop Application. I figure this way I can be more proactive.

我知道我可以在发生异常时明确记录并通知自己,但我必须在任何地方都这样做,而且我可能(更可能会)错过几个.

I know I could just explicitly log and notify myself whenever an exception occurs, but I'd have to do it everywhere and I might(more likely will) miss a couple.

有什么建议吗?

推荐答案

您可能不想邮寄任何异常.JDK 中有很多代码实际上依赖于异常才能正常工作.我认为您更感兴趣的是未捕获的异常.如果您正在捕获异常,您应该在那里处理通知.

You probobly don't want to mail on any exception. There are lots of code in the JDK that actaully depend on exceptions to work normally. What I presume you are more inerested in are uncaught exceptions. If you are catching the exceptions you should handle notifications there.

在桌面应用程序中,有两个地方需要担心这一点,在 (EDT) 和 EDT 之外.在全球范围内,您可以注册一个实现 java.util.Thread.UncaughtExceptionHandler 的类,并通过 java.util.Thread.setDefaultUncaughtExceptionHandler 注册它.如果异常结束到堆栈底部并且该线程尚未在线程或 ThreadGroup 的当前线程实例上设置处理程序,则会调用此方法.

In a desktop app there are two places to worry about this, in the event-dispatch-thread (EDT) and outside of the EDT. Globaly you can register a class implementing java.util.Thread.UncaughtExceptionHandler and register it via java.util.Thread.setDefaultUncaughtExceptionHandler. This will get called if an exception winds down to the bottom of the stack and the thread hasn't had a handler set on the current thread instance on the thread or the ThreadGroup.

EDT 有一个不同的钩子来处理异常.系统属性 'sun.awt.exception.handler' 需要使用具有零参数构造函数的类的完全限定类名进行注册.这个类需要一个实例方法 handle(Throwable) 来完成你的工作.返回类型无关紧要,因为每次都会创建一个新实例,所以不要指望保持状态.

The EDT has a different hook for handling exceptions. A system property 'sun.awt.exception.handler' needs to be registerd with the Fully Qualified Class Name of a class with a zero argument constructor. This class needs an instance method handle(Throwable) that does your work. The return type doesn't matter, and since a new instance is created every time, don't count on keeping state.

因此,如果您不在乎示例中发生的异常是哪个线程,则可能如下所示:

So if you don't care what thread the exception occurred in a sample may look like this:

class ExceptionHandler implements Thread.UncaughtExceptionHandler {
  public void uncaughtException(Thread t, Throwable e) {
    handle(e);
  }

  public void handle(Throwable throwable) {
    try {
      // insert your e-mail code here
    } catch (Throwable t) {
      // don't let the exception get thrown out, will cause infinite looping!
    }
  }

  public static void registerExceptionHandler() {
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
    System.setProperty("sun.awt.exception.handler", ExceptionHandler.class.getName());
  }
}

将这个类添加到一些随机包中,然后调用registerExceptionHandler方法就可以了.

Add this class into some random package, and then call the registerExceptionHandler method and you should be ready to go.

这篇关于如何检测何时在 Java 中全局抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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