摇摆uncaughtexceptionhandler [英] swing uncaughtexceptionhandler

查看:159
本文介绍了摇摆uncaughtexceptionhandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为swing应用程序构建一个常规异常处理程序,如下所述: http: /www.javaspecialists.eu/archive/Issue081.html

I am trying to build a general exception handler for a swing application as described here: http://www.javaspecialists.eu/archive/Issue081.html

我在jython工作(python语法得到编译到java并执行)。我的代码大致如下(更新):

I work in jython (python syntax getting compiled to java and executed). My code looks roughly like this (updated):

def launcher(func):
    class launcherThread(Runnable):
        def __init__(self):
            super(launcherThread, self).__init__()

        def run(self):
            func()

    #trying to get the name which can be used to instantiate this in java
    cls = ExceptionGroup().getClass()
    fullName = cls.__module__ + '.' + cls.__name__

    System.setProperty("sun.awt.exception.handler", fullName)
    Thread(ExceptionGroup(), launcherThread(), 'Cross ExceptionHandlerThread').start()

class ExceptionGroup(ThreadGroup):
    def __init__(self):
         super(ExceptionGroup, self).__init__("HardenedGroup")

    def uncaughtException(self, thread, exception):
        #make a fancy dialog displaying str(exception)

如果我测试它,它可以正常工作,但在产品它会失败。
对于测试,我在Eclipse(PyDev)中启动了我的程序,生产环境是用Java编写的第三方应用程序,其中包含一个Jython控制台。该应用程序支持添加自定义菜单项,并将jython脚本放在这些。

If I test it it works fine however in the production enviornment it failes. For testing I launch my program in Eclipse (PyDev), the production enviornment is a third party application written in Java, that has a Jython console build in. The application supports adding of custom menu entries, and putting jython scripts on these.

我在测试和生产环境之间看到的主要区别是,在生产环境中,摆动线程已经启动(第三方应用程序实现了摆动)。这是否导致我的 ThreadGroup 设置失败,还是有另一个原因,为什么这不工作?

The main difference I see between testing and production enviornment is that in the production enviornment the swing threads are allready started (the third party application utilitizes swing). Does this cause my ThreadGroup setting to fail, or is there another reason why this is not working?

如何我得到涉及的线程(由于ButtonActions引起的异常ar抛出)检查其defaultException处理程序?如果(我恐怕)应该证明第三方安装了自己的处理程序(所有异常都写入一个日志文件),我该怎么做一个新的swing工作线程? (我不想捕获主机应用程序创建的异常)

How can I get the Involved threads (exceptions ar thrown as a result of buttonActions) to check their defaultException handlers? If (as I am afraid) it should turn out that the third party installed its own handler (all exceptions are written to a log file) how can I make a new swing worker thread? (I don't want to catch the exceptions created by the host application after all)

问题回顾:
1.如何检查哪些线程已启动将函数 func 传入启动器函数,并看到它的未捕获的异常处理程序?
2.我可以为我的gui部分和主要应用程序gui部分强制执行一个单独的swing调度程序吗? (如果我在我的添加框架中退出OnClos,第三方应用程序关闭)?

Question recap: 1. How can I check which threads are started for the function func passed into the launcher function and see thier uncaught exception handler? 2. Can I enforce a seperate swing dispatcher for my gui part and the main applications gui part? (If I exitOnClos on a frame of my add in, the third party application closes)?

更新
考虑到anwser从 lbalazscs 我试图使用 sun.awt.exception.handler 属性,但它没有影响,异常仍然在日志文件中(应用程序dfeault行为)。我使用正确吗? (ps:我在Java 1.6)

Update: Considering the anwser from lbalazscs I am trying to use the sun.awt.exception.handler property, but it has no effect, the exceptions still end up in the log file (applications dfeault behaviour). Am I using it right? (p.s.: I am on Java 1.6)

推荐答案

如果你有Java 5或更高版本,你也可以使用Thread.setDefaultUncaughtExceptionHandler ),这也在较新的Java专家通讯中描述:

If you have Java 5 or higher, you can also use Thread.setDefaultUncaughtExceptionHandler(), which is also described in a newer "Java Specialists' Newsletter":

http://www.javaspecialists.eu/archive/Issue089.html

这里是最新的Java 7版本:

And here is the newest Java 7 version:

http:// www .javaspecialists.eu / archive / Issue196.html

另请参阅:
为什么要设置sun.awt.exception.handler属性?

编辑:这是我如何使用Thread.setDefaultUncaughtExceptionHandler(在Java ...):

This is how I use Thread.setDefaultUncaughtExceptionHandler (in Java...):

public static void setupGlobalExceptionHandling() {
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            handleException(e);
        }
    });
}

这篇关于摇摆uncaughtexceptionhandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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