admin管理员组

文章数量:1292140

I'm writing a script that might exit normally in many different points in the program. The script currently always outputs a message to STDOUT in an ensure block, sorta like this:

begin
    exit
ensure
    puts "my message"
end

The problem is that if the script crashes, the message is output anyway, followed by the error messages. What I want to do is only output the message on an exit 0, something like this:

begin
  exit 1
ensure
  if is_this_a_normal_exit?()
    puts 'my messsage'
  end
end

Is there a way to do that?

I'm writing a script that might exit normally in many different points in the program. The script currently always outputs a message to STDOUT in an ensure block, sorta like this:

begin
    exit
ensure
    puts "my message"
end

The problem is that if the script crashes, the message is output anyway, followed by the error messages. What I want to do is only output the message on an exit 0, something like this:

begin
  exit 1
ensure
  if is_this_a_normal_exit?()
    puts 'my messsage'
  end
end

Is there a way to do that?

Share Improve this question asked Feb 13 at 11:01 tscheingeldtscheingeld 88710 silver badges24 bronze badges 1
  • 1 save the wanted status in a var and use it in the ensure block? – Fravadona Commented Feb 13 at 11:31
Add a comment  | 

1 Answer 1

Reset to default 2

When calling exit, this is generally Kernel#exit. This method basically throws a SystemExit exception which may eventually result in the program being exited.

You can either rescue this exception or check it in the ensure block:

begin
  exit
rescue SystemExit => e
  puts "my message" if e.success?

  # re-raise the error to actually cause the program to exit
  raise 
ensure
  # Here you can still add code which is executed after handling
  # (or not handling) any exceptions. If there was an exception,
  # you can access it here with the $! global variable.
end

Note that there are other possible causes of a program being exited, some of which you can't catch at all (e.g. when calling Kernel#exit! or when you or some other process sends a signal like SIGKILL to kill your program). Also, when using threads, the SystemExit exception may not be propagated to the main thread.

本文标签: rubyGetting exit status in ensure blockStack Overflow