Bug #1886

Strange exception thrown when Exception#new is overridden and returns a non-Exception

Added by Tomas Matousek over 2 years ago. Updated 10 months ago.

[ruby-core:24767]
Status:Closed Start date:08/05/2009
Priority:Normal Due date:
Assignee:Kirk Haines % Done:

100%

Category:-
Target version:-
ruby -v:ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32]

Description

class ZeroDivisionError
  def self.new(message)
    puts 'new ZeroDivError'
    'not an exception'
  end
end

module Any
  def self.===(other)
    puts "?#{other.inspect}"
    true
  end
end

begin
  1/0
rescue Exception, Any
  puts "rescue"
  p $!
ensure
  puts "ensure"
end

puts 'Done'

=====

The output is:
new ZeroDivError
ensure

This means that if a non-Exception is returned from the new method above, the stack is magically unwound ignoring all rescue clauses (=== is not called) and no error message is displayed at all.

The same behavior can be observed in both:
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32]

Associated revisions

Revision 24403
Added by Nobuyoshi Nakada over 2 years ago

* eval.c (rb_exc_raise, rb_exc_fatal): require exception object. [ruby-core:24767]

Revision 24404
Added by Nobuyoshi Nakada over 2 years ago

* bootstraptest/test_exception.rb: test for [ruby-core:24767].

Revision 28374
Added by wyhaines over 1 year ago

eval.c: Bug #1886 [ruby-core:24767]; ensure that rb_exc_raise and rb_exc_fatal require an exception object. Backport of r24403. test/ruby/test_exception.rb: test for exception change. Backport of r24404.

History

Updated by Nobuyoshi Nakada over 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
Applied in changeset r24403.

Updated by Shyouhei Urabe over 2 years ago

  • Status changed from Closed to Assigned
  • Assignee set to Kirk Haines

Updated by Kirk Haines over 1 year ago

  • Status changed from Assigned to Closed
This issue was solved with changeset r28374.
Tomas, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF