OK, I have added a RSpec test, and  have already tagged it as an IronRuby b=
ug.

For reference, .NET allows the equivalent ThreadAbortException to be caught=
, but automatically rethrows it at the end of the catch/rescue block. The p=
rogrammer is required to take a separate action (calling Thread.ResetAbort<=
http://msdn.microsoft.com/en-us/library/system.threading.thread.resetabort.=
aspx>) to complete cancel the abort operation. This prevents situations whe=
re one component triggers the abort, and another cancels it unintentially. =
In theory, you should always do "rescue SomeSpecificExceptionType", but a l=
ot of people do use just "rescue".

From: Evan Phoenix [mailto:evan / fallingsnow.net]
Sent: Monday, January 12, 2009 1:01 PM
To: ruby-core / ruby-lang.org
Subject: [ruby-core:21290] Re: MRI 1.8.6 bug - exit is ignored if ensure cl=
ause throws exception which is caught by outer rescue


On Jan 12, 2009, at 12:31 PM, Shri Borde wrote:


The following code snippet does print "after foo" when using MRI 1.8.6 on W=
indows Vista, showing that exit is ignored. Any thoughts on whether this is=
 by design, a known bug, or a new issue? If it's the latter, should I open =
a bug for it?

No, this is by design. Kernel#exit raises SystemExit, which the toplevel re=
scues and performs the actual exit. If you raise a new exception in the ens=
ure. the SystemExit exception is never seen by the toplevel, and thus you d=
on't get the exit.

This is used in a few places to cause exit to be ignored, or to allow a lib=
rary to convert a SystemExit exception into something else (perhaps an Ille=
galOperation or something).

 - Evan Phoenix



def foo
  begin
    begin
      begin
        exit
      rescue
        puts "We never reach here"
      end
    ensure
      raise "exception from ensure"
    end
  rescue
    puts "We do reach here, which is unexpected"
  end
end

foo
print "after foo"

Thanks,
Shri