rubyexceptionrescue

How to abort a Ruby script when raising an Exception?


Is it possible, in Ruby, to raise an Exception that will also automatically abort the program, ignoring any enclosing begin/rescue blocks?


Solution

  • Unfortunately, none of these exit answers will work. exit raises SystemExit which can be caught. Observe:

    begin
      exit
    rescue SystemExit
    end
    
    puts "Still here!"
    

    As @dominikh says, you need to use exit! instead:

    begin
      exit!
    rescue SystemExit
    end
    
    puts "Didn't make it here :("