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

查看:234
本文介绍了如何检测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.

在桌面应用程序中,有两个地方需要担心这一点,在 event-dispatch-thread (EDT)and outside of of美国东部时间。 Globaly你可以注册一个实现 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'需要使用零参数构造函数的类的完全限定类名注册。这个类需要一个实现你的工作的实例方法句柄( 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天全站免登陆