From: Martin Hansen Date: 2010-08-06T18:58:45+09:00 Subject: Re: Abstracting exception handling > What do you want the exception handler to do? Will the program still > terminate afterwards? I want to write the exception type to a log file and terminate the script. $! does not tell the difference between interrupt, terminate, and quit (perhaps that could be fixed? - just a thought). > If so, why not just add an at_exit handler? I have been messing around with this for a while, but I have failed to get it right - and now I am utterly confused. If I have test.rb: #!/usr/bin/env ruby require 'my_aux_class' Greet.new raise "raise from main" And my_aux_class.rb: class Greet def initialize puts "Hello World" end end at_exit { puts "Last exception was #{$!.inspect}" if $! } begin exit_status = "OK" %w( INT TERM QUIT ).each do |signal| Signal.trap(signal) do exit_status = signal exit end end raise "raise from aux" rescue Exception => exception puts "rescuing #{exception}" puts exception.backtrace ensure puts "exit status: #{exit_status}" end Running this I get the following output: rescuing raise from aux /Users/maasha/my_aux_class.rb:21:in `' ./test.rb:3:in `require' ./test.rb:3:in `
' exit status: OK Hello World Last exception was # ./test.rb:7:in `
': raise from main (RuntimeError) I actually wanted the raise from main rescued, but the begin/rescue/ensure is misplaced/misused :o( Martin -- Posted via http://www.ruby-forum.com/.