Saturday, June 19, 2010

re-check "begin-rescue-else-end" on your ruby environment

I found some bug report for JRuby in here (now fixed).

I copy and simplify this sample code like below:
def hoge
  puts 1
rescue Exception => e
  puts 2
  raise "shouldn't get here"
else
  puts 3
  raise "should raise out of the method"
end
begin
  puts RUBY_VERSION
  hoge
rescue => e
  puts e.message
end

Let's evaluate this on each ruby environment.

Ruby 1.8.7
irb(main):001:0> def hoge
irb(main):002:1>   puts 1
irb(main):003:1> rescue Exception => e
irb(main):004:1>   puts 2
irb(main):005:1>   raise "shouldn't get here"
irb(main):006:1> else
irb(main):007:1*   puts 3
irb(main):008:1>   raise "should raise out of the method"
irb(main):009:1> end
=> nil
irb(main):010:0> begin
irb(main):011:1*   puts RUBY_VERSION
irb(main):012:1>   hoge
irb(main):013:1> rescue => e
irb(main):014:1>   puts e.message
irb(main):015:1> end
1.8.7
1
3
should raise out of the method
=> nil

Ruby 1.9.1
irb(main):001:0> def hoge
irb(main):002:1>   puts 1
irb(main):003:1> rescue Exception => e
irb(main):004:1>   puts 2
irb(main):005:1>   raise "shouldn't get here"
irb(main):006:1> else
irb(main):007:1*   puts 3
irb(main):008:1>   raise "should raise out of the method"
irb(main):009:1> end
=> nil
irb(main):010:0> begin
irb(main):011:1*   puts RUBY_VERSION
irb(main):012:1>   hoge
irb(main):013:1> rescue => e
irb(main):014:1>   puts e.message
irb(main):015:1> end
1.9.1
1
3
should raise out of the method
=> nil

Ruby 1.9.2 pleview 3
irb(main):001:0> def hoge
irb(main):002:1>   puts 1
irb(main):003:1> rescue Exception => e
irb(main):004:1>   puts 2
irb(main):005:1>   raise "shouldn't get here"
irb(main):006:1> else
irb(main):007:1*   puts 3
irb(main):008:1>   raise "should raise out of the method"
irb(main):009:1> end
=> nil
irb(main):010:0> begin
irb(main):011:1*   puts RUBY_VERSION
irb(main):012:1>   hoge
irb(main):013:1> rescue => e
irb(main):014:1>   puts e.message
irb(main):015:1> end
1.9.2
1
3
should raise out of the method
=> nil

JRuby 1.5.1
irb(main):001:0> def hoge
irb(main):002:1>   puts 1
irb(main):003:1> rescue Exception => e
irb(main):004:1>   puts 2
irb(main):005:1>   raise "shouldn't get here"
irb(main):006:1> else
irb(main):007:1*   puts 3
irb(main):008:1>   raise "should raise out of the method"
irb(main):009:1> end
=> nil
irb(main):010:0> begin
irb(main):011:1*   puts RUBY_VERSION
irb(main):012:1>   hoge
irb(main):013:1> rescue => e
irb(main):014:1>   puts e.message
irb(main):015:1> end
1.8.7
1
3
2
shouldn't get here
=> nil

IronRuby 1.0
>>> def hoge
...   puts 1
... rescue Exception => e
...   puts 2
...   raise "shouldn't get here"
... else
...   puts 3
...   raise "should raise out of the method"
... end
=> nil
>>> begin
...   puts RUBY_VERSION
...   hoge
... rescue => e
...   puts e.message
... end
1.8.6
1
3
should raise out of the method
=> nil

Fortunately, above case is only on JRuby 1.5.0, 1.5.1, and this maybe be fixed at next version of JRuby.

No comments:

Post a Comment