Java exceptions can't be rescued with "rescue Exception" --------------------------------------------------------
Key: JRUBY-4677 URL: http://jira.codehaus.org/browse/JRUBY-4677 Project: JRuby Issue Type: Bug Components: Java Integration Affects Versions: JRuby 1.4 Reporter: Charles Oliver Nutter Fix For: JRuby 1.5 You can rescue Java exceptions like you would Ruby exceptions: {noformat} ~/projects/jruby ➔ jruby -rjava -e "begin; raise java.lang.NullPointerException.new; rescue java.lang.NullPointerException; puts 'rescued'; end" rescued {noformat} And you can rescue exceptions that come out of Java code, which get wrapped in a NativeException Ruby object: {noformat} ~/projects/jruby ➔ jruby -rjava -e "begin; raise java.lang.System.get_property(nil); rescue Exception => e; puts 'rescued: ' + e.class.to_s; end" rescued: NativeException {noformat} So there's some duality here; we don't wrap all Java exception, and have wanted to move away from wrapping *any* Java exceptions if possible while unifying exception handling. So the problem is that non-wrapped Java exceptions don't extend Ruby's Exception, and so rescue Exception won't pick them up without some special handling: {noformat} ~/projects/jruby ➔ jruby -rjava -e "p java.lang.NullPointerException.ancestors"[Java::JavaLang::NullPointerException, Java::JavaLang::RuntimeException, Java::JavaLang::Exception, Java::JavaLang::Throwable, Java::JavaIo::Serializable, Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods, Object, Kernel] {noformat} So I see a few options: 1. Make rescue Exception rescue Java Throwable as well. This will be tricky, because we don't want to intercept exceptions we use for flow-control, but we already special-case them anyway. It's also a little problematic to me because you'll rescue Exception but get something that's not an Exception. 2. Try some type trickery and make all Java exceptions extend Ruby Exception in some way. I'm not sure how this would work, and it would certainly change how they appear to Ruby code. 3. Require that people attempting to rescue everything rescue Object, which would pick up Java exceptions. This seems like the least desirable, since only in JRuby would you have to do that. -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://jira.codehaus.org/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email