Is SystemExit a special kind of Exception?

前端 未结 2 1066
北海茫月
北海茫月 2020-12-17 15:30

How does SystemExit behave differently from other Exceptions? I think I understand some of the reasoning about why it wouldn\'t be good t

2条回答
  •  执笔经年
    2020-12-17 16:30

    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]

提交回复
热议问题