Skip to content

Java exceptions not caught by bare rescue #3620

@mohamedhafez

Description

@mohamedhafez

I made a simple Java class that raises a java exception just to see how TruffleRuby handles them, and ran the following code in IRB:

irb(main):002:0>   Java.import "MyJavaTest"
=> #<Polyglot::ForeignClass[Java] type MyJavaTest>
irb(main):003:0> r=nil
=> nil
irb(main):004:1> begin
irb(main):005:1>   MyJavaTest.test_java_exception
irb(main):006:1> rescue
irb(main):007:1>   puts "in rescue: #{$!.class}"
irb(main):008:1>   r = $!
irb(main):009:0> end
MyJavaTest.java:5:in `test_java_exception': / by zero (Polyglot::ForeignException: java.lang.ArithmeticException)
        from (irb):2:in `<main>'         
        from <internal:core> core/kernel.rb:409:in `Kernel#loop'
        from <internal:core> core/throw_catch.rb:36:in `Kernel#catch'
	from <internal:core> core/throw_catch.rb:36:in `Kernel#catch'
	from /Users/mohamed/.rbenv/versions/truffleruby+graalvm-24.0.1/lib/gems/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:378:in `Kernel#load'
	from /Users/mohamed/.rbenv/versions/truffleruby+graalvm-24.0.1/bin/irb:44:in `<main>'
irb(main):010:0> 
irb(main):011:0> r
=> nil
irb(main):012:0> 

Under JRuby I can then access that exception object in r, and with Ruby exceptions of course the exception object is available in r

I'm running JVM standalone truffleruby:
truffleruby 24.0.1, like ruby 3.2.2, Oracle GraalVM JVM [aarch64-darwin]

MyJavaTest.java:

public class MyJavaTest {
    public static int test_java_exception() {
        int result = 12 / 0;
        return result;
    }
}

Metadata

Metadata

Assignees

Labels

polyglotUses multiple Truffle languages or host interop

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions