From: "Iñaki Baz Castillo" Date: 2011-02-02T19:49:56+09:00 Subject: Re: How to know the exit status within at_exit() block? 2011/2/1 Robert Klemme : > You are aware that you can simplify that to > > exit SOMETHING > > are you? Yes, but I want to inspect the exit status code into the block provided to at_exit(). >> Of course I call "exit" from lof of clases/modules under my project so >> I cannot use a local scope variable "status" (and I wouldn't like to >> use a global variable). > > First of all: IMHO that is bad practice because it prevents your code > to be used in different situations (i.e. reduces modularity).  The > proper way would be to raise exceptions and deal with them > appropriately.  You can even omit the handling on top level and get > appropriate exit codes of the process (i.e. 0 for the regular case, 1 > for error): > > $ ruby19 -e 'raise "f"'; echo $? > -e:1:in `
': f (RuntimeError) > 1 > $ ruby19 -e 'nil'; echo $? > 0 > $ > > However, you can do what you want with a rescue clause: > > begin >  # main code > rescue SystemExit => e >  puts "exiting with status #{e.status}" >  raise > rescue Exception => e >  puts "exiting with status 1" >  raise > else >  puts "exiting with status 0" > end Using "rescue SystemExit => e" seems indeed appropriate. Let me play a bit with it. Thanks a lot. -- Iñaki Baz Castillo