Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rescuing foreign exceptions #2544

Closed
eregon opened this issue Dec 6, 2021 · 2 comments
Closed

Support rescuing foreign exceptions #2544

eregon opened this issue Dec 6, 2021 · 2 comments
Assignees
Labels
Milestone

Comments

@eregon
Copy link
Member

eregon commented Dec 6, 2021

Reported by @jylamont in https://graalvm.slack.com/archives/CMY63522F/p1637250173107100

Like rescue Java.type('org.foo.SomeException').

rescue Java.type('org.postgresql.util.PSQLException') should work but it does not currently.


It's unclear if a lone rescue should rescue foreign exceptions.

JRuby behavior seems to treat StandardError specially as an ad-hoc way to support lone rescue.
I am not sure we want to follow that.

~ ∴ ruby -e 'begin; p java.lang.Integer.valueOf("abc"); rescue => e; p e; p e.class; p e.class.ancestors; end'          
java.lang.NumberFormatException: For input string: "abc"
Java::JavaLang::NumberFormatException
[Java::JavaLang::NumberFormatException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::RuntimeException, Java::JavaLang::Exception, Java::JavaLang::Throwable, Java::JavaIo::Serializable, Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods, Object, Kernel, BasicObject]

~ ∴ ruby -e 'begin; p java.lang.Integer.valueOf("abc"); rescue RuntimeError => e; p e; p e.class; p e.class.ancestors; end' 
Unhandled Java exception: java.lang.NumberFormatException: For input string: "abc"
java.lang.NumberFormatException: For input string: "abc"
                     forInputString at java/lang/NumberFormatException.java:65
                           parseInt at java/lang/Integer.java:652
                            valueOf at java/lang/Integer.java:983
                            invoke0 at jdk/internal/reflect/NativeMethodAccessorImpl.java:-2
                             invoke at jdk/internal/reflect/NativeMethodAccessorImpl.java:62
                             invoke at jdk/internal/reflect/DelegatingMethodAccessorImpl.java:43
                             invoke at java/lang/reflect/Method.java:566
  invokeDirectWithExceptionHandling at org/jruby/javasupport/JavaMethod.java:456
                 invokeStaticDirect at org/jruby/javasupport/JavaMethod.java:368
               invokeOther3:valueOf at -e:1
                             <main> at -e:1
                                run at -e:-1
                invokeWithArguments at java/lang/invoke/MethodHandle.java:710
                          runScript at org/jruby/Ruby.java:1257
                        runNormally at org/jruby/Ruby.java:1176
                        runNormally at org/jruby/Ruby.java:1158
                        runNormally at org/jruby/Ruby.java:1194
                        runFromMain at org/jruby/Ruby.java:977
                      doRunFromMain at org/jruby/Main.java:400
                        internalRun at org/jruby/Main.java:292
                                run at org/jruby/Main.java:234
                               main at org/jruby/Main.java:206

~ ∴ ruby -e 'begin; p java.lang.Integer.valueOf("abc"); rescue StandardError => e; p e; p e.class; p e.class.ancestors; end'
java.lang.NumberFormatException: For input string: "abc"
Java::JavaLang::NumberFormatException
[Java::JavaLang::NumberFormatException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::RuntimeException, Java::JavaLang::Exception, Java::JavaLang::Throwable, Java::JavaIo::Serializable, Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods, Object, Kernel, BasicObject]

~ ∴ ruby -e 'begin; p java.lang.Integer.valueOf("abc"); rescue Exception => e; p e; p e.class; p e.class.ancestors; end'
java.lang.NumberFormatException: For input string: "abc"
Java::JavaLang::NumberFormatException
[Java::JavaLang::NumberFormatException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::RuntimeException, Java::JavaLang::Exception, Java::JavaLang::Throwable, Java::JavaIo::Serializable, Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods, Object, Kernel, BasicObject]

So all host exceptions seem to be considered StandardError or so, even though the ancestors do not reflect that.
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#exception-handling

Internal issue: GR-34933

@eregon eregon added the polyglot label Dec 6, 2021
@eregon eregon added this to the 22.1.0 milestone Feb 28, 2022
@eregon eregon self-assigned this Feb 28, 2022
@eregon
Copy link
Member Author

eregon commented Mar 2, 2022

This is now implemented: e6753ad

@eregon eregon closed this as completed Mar 2, 2022
@jylamont
Copy link

jylamont commented Mar 2, 2022

Thanks for this @eregon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants