Message7389

Author paulfernhout
Recipients fwierzbicki, paulfernhout
Date 2012-08-11.17:54:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1344707701.46.0.00628409587505.issue1256374@psf.upfronthosting.co.za>
In-reply-to
Content
Frank, 

First, thanks for all the great work you've done with Jython, and thanks for getting around to snipping off all these loose ends. I really enjoyed my time using Jython, but I've been doing mostly straight Java work myself lately, so I don't have time or interest to push this issue forward much myself at this point, sorry. This issue was with the Jython version from about seven years ago and running on Java 1.4 (the issue being posted in 2005), so I am not sure how reproducible this issue is at this point or whether the suggestion for resolving it is the best one anymore. However, here are a few comments on the general issue that might be useful to any Jython user encountering this issue or trying to resolve it in some way. These comments suggest you can just mark the issue closed (at least with a likely workaround) at this point.

Later versions of Java have new ways of dealing with uncaught exceptions, like in Java 5, which introduced setDefaultUncaughtExceptionHandler for Thread:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29

Still, since the Swing thread has its own exception handler that prints to the console, something else might still need to be done for a Java-based application to intercept such Swing exceptions. Apparently, according to this StackOverflow question, you still need to set the "sun.awt.exception.hander" property in some situations, even in Java 6:
http://stackoverflow.com/questions/5794472/why-bother-with-setting-the-sun-awt-exception-handler-property

So, I don't think using the above by itself would fix this issue. Still, clearly you can handle Swing Event Dispatch Thread exceptions from plain Java, like discussed in these Stack Overflow questions: 
http://stackoverflow.com/questions/4448523/how-can-i-catch-event-dispatch-thread-edt-exceptions
http://stackoverflow.com/questions/95767/how-can-i-catch-awt-thread-exceptions-in-java

It is suggested in an answer at the last link that Java7 may have fixed this, so that the uncaught exception handler will cover everything. 

I'm also not quite sure what happens if you try to get hold of the EDT instance directly and call setUncaughtExceptionHandler directly (perhaps by reflection if needed)? However, it seems like Java6 will not allow that, according to this JDK bug report / feature request?
http://bugs.sun.com/view_bug.do?bug_id=6791501

In any case, seven years later, I can hope this could be done entirely from Jython at this point for Java 5 or Java 6? Certainly I would expect it would work from Java 7. 

The big issue back then was that I could not figure out how to get a valid class name string to send into that call to System.setProperty which referenced a Jython-defined exception handler class. Otherwise, a Jython application could then just call System.setProperty directly of course (and that part of the patch probably did not have to be in Java even then). However, I have not tried the latest versions of Jython to know if you could define a class in such a way that the JVM would notice it later. If so, you could set the Event Dispatch Thread hook in the regular way for "sun.awt.exception.handler". But if Jython could do that now (or if perhaps it could do it then and I did not understand how to do that then), then there would be no need for the change I outlined back then.

In that sense, maybe this was (or still is?) more a "bug" about the way Jython defined Java-accessible class names then? Or at least a "bug" in my understanding of how to to pass a string to Java with the full name of a Jython-defined class? You might be the best person to be able to answer off-hand whether a Jython-defined class could looked up by name in the current version of Jython. If there was a way to do that, and it worked when setting this System property, then this patch would be unneeded.

Also, if a new Java-defined exception handling class was for some reason was needed in the end (I'm not sure how Java looks for named classes that are defined later), then it might be possible that a developer could include such a class in a Jar file in the classpath when starting Jython rather than by modifying the Jython codebase itself? I have not tried that though, but I would expect it might work. So, if using a jar file was combined with calling System.setProperty from Jython, I would think there is at the very least a workaround (although I have not tested that). I can wonder if there is also possible that this is an issue of timing -- that once the EDT is started in those earlier versions of Java, maybe it was too late to set that property?

So, probably there are a few plausible workarounds for this issue (including just waiting for Java 7 to become mainstream). In that sense, it's fine by me if you just mark this issue closed. 

--Paul Fernhout
http://www.pdfernhout.net/
====
The biggest challenge of the 21st century is the irony of technologies of abundance in the hands of those thinking in terms of scarcity.
History
Date User Action Args
2012-08-11 17:55:01paulfernhoutsetmessageid: <1344707701.46.0.00628409587505.issue1256374@psf.upfronthosting.co.za>
2012-08-11 17:55:01paulfernhoutsetrecipients: + paulfernhout, fwierzbicki
2012-08-11 17:55:01paulfernhoutlinkissue1256374 messages
2012-08-11 17:54:59paulfernhoutcreate