Is it possible, in Ruby, to raise an Exception that will also automatically abort the program, ignoring any enclosing begin/rescue blocks?
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 :("
Edu already asked: If you want to abort the program, why not go straight for it and use 'exit'
One Possibility: You may define your own Exception and when the exception is called, the exception stops the program with exit:
class MyException < StandardError
#If this Exception is created, leave program.
def initialize
exit 99
end
end
begin
raise MyException
rescue MyException
puts "You will never see meeeeeee!"
end
puts "I will never get called neither :("
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With