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 :("
begin
puts 'Hello'
# here, instead of raising an Exception, just exit.
exit
puts "You will never see meeeeeee!"
rescue # whatever Exception
# ...
end
puts "I will never get called neither :("
4条答案
按热度按时间vbopmzt11#
不幸的是,这些
exit
答案都不起作用。exit
引发SystemExit
,可以捕获。观察:正如@dominikh所说,你需要使用
exit!
来代替:e7arh2l62#
埃杜已经问过了:* 如果你想中止程序,为什么不直接使用“退出”*
一种可能:你可以定义自己的Exception,当异常被调用时,异常会通过exit停止程序:
9njqaruj3#
这能满足你的要求吗
zynd9foi4#
我的回答与Maran的相似,但略有不同: