SystemExit 是一种特殊的异常吗?

发布于 2024-10-19 03:33:59 字数 296 浏览 3 评论 0原文

SystemExit 的行为与其他 Exception 有何不同?我想我理解为什么提出适当的异常不好的一些原因。例如,您不希望发生这样的奇怪事情:

begin
  exit
rescue => e
  # Silently swallow up the exception and don't exit
end

但是如何救援会忽略SystemExit? (它使用什么标准?)

How does SystemExit behave differently from other Exceptions? I think I understand some of the reasoning about why it wouldn't be good to raise a proper Exception. For example, you wouldn't want something strange like this to happen:

begin
  exit
rescue => e
  # Silently swallow up the exception and don't exit
end

But how does the rescue ignore SystemExit? (What criteria does it use?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2024-10-26 03:33:59

当您编写没有一个或多个类的 rescue 时,它与编写相同

begin
  ...
rescue StandardError => e
  ...
end

但是,有些异常不是从 StandardError 继承的。 SystemExit 是其中之一,因此不会捕获它。以下是 Ruby 1.9.2 中层次结构的子集,您可以自己查找< /a>:

BasicObject
  Exception
    NoMemoryError
    ScriptError
      LoadError
        Gem::LoadError
      NotImplementedError
      SyntaxError
    SecurityError
    SignalException
      Interrupt
    StandardError
      ArgumentError
      EncodingError
        Encoding::CompatibilityError
        Encoding::ConverterNotFoundError
        Encoding::InvalidByteSequenceError
        Encoding::UndefinedConversionError
      FiberError
      IOError
        EOFError
      IndexError
        KeyError
        StopIteration
      LocalJumpError
      NameError
        NoMethodError
      RangeError
        FloatDomainError
      RegexpError
      RuntimeError
      SystemCallError
      ThreadError
      TypeError
      ZeroDivisionError
    SystemExit
    SystemStackError
    fatal

因此,您可以使用以下方式捕获 SystemExit

begin
  ...
rescue SystemExit => e
  ...
end

...或者您可以选择捕获每个异常,包括SystemExit with:

begin
  ...
rescue Exception => e
  ...
end

自己尝试一下:

begin
  exit 42
  puts "No no no!"
rescue Exception => e
  puts "Nice try, buddy."
end
puts "And on we run..."

#=> "Nice try, buddy."
#=> "And on we run..."

请注意,此示例在(某些版本的?)IRB 中不起作用,IRB 提供了自己的退出方法来屏蔽正常的 Object#exit。

在 1.8.7 中:

method :exit
#=> #<Method: Object(IRB::ExtendCommandBundle)#exit>

在 1.9.3 中:

method :exit
#=> #<Method: main.irb_exit>

When you write rescue without one or more classes, it is the same as writing:

begin
  ...
rescue StandardError => e
  ...
end

There are Exceptions that do not inherit from StandardError, however. SystemExit is one of these, and so it is not captured. Here is a subset of the hierarchy in Ruby 1.9.2, which you can find out yourself:

BasicObject
  Exception
    NoMemoryError
    ScriptError
      LoadError
        Gem::LoadError
      NotImplementedError
      SyntaxError
    SecurityError
    SignalException
      Interrupt
    StandardError
      ArgumentError
      EncodingError
        Encoding::CompatibilityError
        Encoding::ConverterNotFoundError
        Encoding::InvalidByteSequenceError
        Encoding::UndefinedConversionError
      FiberError
      IOError
        EOFError
      IndexError
        KeyError
        StopIteration
      LocalJumpError
      NameError
        NoMethodError
      RangeError
        FloatDomainError
      RegexpError
      RuntimeError
      SystemCallError
      ThreadError
      TypeError
      ZeroDivisionError
    SystemExit
    SystemStackError
    fatal

You can thus capture just SystemExit with:

begin
  ...
rescue SystemExit => e
  ...
end

...or you can choose to capture every exception, including SystemExit with:

begin
  ...
rescue Exception => e
  ...
end

Try it yourself:

begin
  exit 42
  puts "No no no!"
rescue Exception => e
  puts "Nice try, buddy."
end
puts "And on we run..."

#=> "Nice try, buddy."
#=> "And on we run..."

Note that this example will not work in (some versions of?) IRB, which supplies its own exit method that masks the normal Object#exit.

In 1.8.7:

method :exit
#=> #<Method: Object(IRB::ExtendCommandBundle)#exit>

In 1.9.3:

method :exit
#=> #<Method: main.irb_exit>
孤独患者 2024-10-26 03:33:59

简单示例:

begin
  exit
  puts "never get here"
rescue SystemExit
  puts "rescued a SystemExit exception"
end

puts "after begin block"

退出 status / success? 等也可以读取:

begin
  exit 1
rescue SystemExit => e
  puts "Success? #{e.success?}" # Success? false
end

begin
  exit
rescue SystemExit => e
  puts "Success? #{e.success?}" # Success? true
end

方法的完整列表:[:status, :success?, :exception, :消息,:backtrace,:backtrace_locations,:set_backtrace,:原因]

Simple example:

begin
  exit
  puts "never get here"
rescue SystemExit
  puts "rescued a SystemExit exception"
end

puts "after begin block"

The exit status / success?, etc. can be read too:

begin
  exit 1
rescue SystemExit => e
  puts "Success? #{e.success?}" # Success? false
end

begin
  exit
rescue SystemExit => e
  puts "Success? #{e.success?}" # Success? true
end

Full list of methods: [:status, :success?, :exception, :message, :backtrace, :backtrace_locations, :set_backtrace, :cause]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文