如何在引发异常时中止Ruby脚本?

时间:2022-01-25 20:18:32

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

在Ruby中,是否可能引发一个异常,该异常也将自动中止程序,忽略任何封闭的begin/rescue块?

4 个解决方案

#1


7  

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:

正如@dominikh所说,您需要使用exit!而不是:

begin
  exit!
rescue SystemExit
end

puts "Didn't make it here :("

#2


1  

Edu already asked: If you want to abort the program, why not going straight for it and use 'exit'

Edu已经问过了,如果你想中止这个程序,为什么不直接使用exit呢

One Possibility: You may define your own Exception and when the exception is called, the exception stopps the programm with exit:

一种可能:您可以定义自己的异常,当异常被调用时,异常终止程序并退出:

class MyException < StandardError
  #If this Exception is created, leave programm.
  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 :("

#3


0  

Would this do what you want?

这能做你想做的吗?

begin
  puts Idontexist
rescue StandardError
  exit
  puts "You will never see meeeeeee!"
end
puts "I will never get called neither :("

#4


0  

My answer is similar to Maran's one, but slightly different:

我的回答与马兰的相似,但略有不同:

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 :("

#1


7  

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:

正如@dominikh所说,您需要使用exit!而不是:

begin
  exit!
rescue SystemExit
end

puts "Didn't make it here :("

#2


1  

Edu already asked: If you want to abort the program, why not going straight for it and use 'exit'

Edu已经问过了,如果你想中止这个程序,为什么不直接使用exit呢

One Possibility: You may define your own Exception and when the exception is called, the exception stopps the programm with exit:

一种可能:您可以定义自己的异常,当异常被调用时,异常终止程序并退出:

class MyException < StandardError
  #If this Exception is created, leave programm.
  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 :("

#3


0  

Would this do what you want?

这能做你想做的吗?

begin
  puts Idontexist
rescue StandardError
  exit
  puts "You will never see meeeeeee!"
end
puts "I will never get called neither :("

#4


0  

My answer is similar to Maran's one, but slightly different:

我的回答与马兰的相似,但略有不同:

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 :("