From: Brian Candler Date: 2011-12-25T07:41:45+09:00 Subject: Re: Kernel#exit raises an exception? Khat Harr wrote in post #1038120: > While I was working on embedding an interpreter I wrote a function to > catch unhandled exceptions and spit out some information on the > exception. While testing I noticed that the Kernel's 'exit' method > actually seems to raise an exception. > > Is this intended behavior? $ ri Kernel#exit Initiates the termination of the Ruby script by raising the +SystemExit+ exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment. > It struck me as somewhat odd and I had to > write a handler for it in my code (which is a pain in the bum if for no > other reason than it seems to lack elegance). Firstly: you can always call 'exit!' instead of 'exit', which bypasses at-exit handlers and the rescuable exception. Secondly: it is easy to tell Ruby to re-raise SystemExit, which means it will take the standard action (of exiting with the given status code) begin exit 123 rescue SystemExit raise rescue Exception puts 'Other exceptions handled here' end Run this, echo $? and you'll see the status 123. -- Posted via http://www.ruby-forum.com/.