2

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

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)

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.

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?

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)

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)?

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)

ted
  • 4,791
  • 5
  • 38
  • 84

1 Answers1

4

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

And here is the newest Java 7 version:

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

Also see this: Why bother with setting the "sun.awt.exception.handler" property?

EDIT: 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);
        }
    });
}
Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • Since I use java 6 I assume you pointed me in the right direction with . However I have another issue now, I can't get the "sun.awt.exception.handler" porperty to work. I updated the code but it has no effect (see updated question). Since I am trying to catch unhandled exceptions form different threads (including the edt) the `setDefaultUncaughtExceptionHandler()` seems inappropiate to me. – ted Aug 20 '12 at 08:40
  • 1
    With Thread.setDefaultUncaughtExceptionHandler you can set the uncaught exception handling for all threads (is is a static method). See my updated answer. – lbalazscs Aug 20 '12 at 09:45
  • thank you very much, this still leaves the property issue (I need to set it I guess), but I don't think it causes any effect (exceptions still pass by my exception handler and run the default way),... – ted Aug 20 '12 at 10:04
  • I would not rely on "sun.awt.exception.handler", because such code won't work in Java 7: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4714232 – lbalazscs Aug 21 '12 at 09:18
  • the question is not if I want to rely on it (I have the UncaughtExceptionHandler in place) but if I need it. The reason I need it is in your answer: [Why bother with setting the "sun.awt.exception.handler" property?](http://stackoverflow.com/questions/5794472/why-bother-with-setting-the-sun-awt-exception-handler-property). I assume that the fact that I don't catch some exceptions (those generated from gui launched code (e.g. buttonclick action)). Unfortunately I am currently forced to use Java 1.6 and can't upgrade to 1.7 easily. And 1.6 users should see the exception too... – ted Aug 21 '12 at 10:54
  • Thanks to your link I got my `solution`: During startup the third party program generates an uncaught exception. Here is a quote from the [bugreport you linked](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4714232): `Another problem with the system property is that it needs to be set before the first exception gets thrown. After that, it won't have any effect (the handler is cached - for performance reasons, I guess).` – ted Aug 21 '12 at 10:56
  • P.P.S.: Actually there was also another problem, forcing me to use the `Thread.setDefaultUncaughtExceptionHandler`, the swing thread is created by the third party application thus not inheriting the thread group and its exception handler. Thanks again. – ted Aug 28 '12 at 14:58