Rescue exit

Posted by Jonathan

While debugging an unrelated problem in Nanite I've stumbled over this feature in Ruby:

You can rescue the call to Kernel.exit.

Calling exit will just raise a SystemExit exception that can catched as any other.

#!/usr/bin/env ruby

begin
  exit
rescue SystemExit => e
  puts "somebody wanted to #{e}"
end

puts "after exiting"

This is clearly documented in the RDoc but still I was surprised.