From: Brian Candler Date: 2010-08-06T17:39:42+09:00 Subject: Re: Abstracting exception handling Martin Hansen wrote: > Now, I would like error handling to be invoked, if possible from > biopieces.rb, in the case some exception is raised (either by raise or > ctrl-c, etc) What do you want the exception handler to do? Will the program still terminate afterwards? If so, why not just add an at_exit handler? --- prog1.rb --- at_exit { puts "Last exception was #{$!.inspect}" if $! } raise "Boom" --- prog2.rb --- at_exit { puts "Last exception was #{$!.inspect}" if $! } puts "Nothing happened" --- prog3.rb --- at_exit { puts "Last exception was #{$!.inspect}" if $! } begin raise "Caught it" rescue RuntimeError end puts "Nothing happened" --- prog4.rb --- at_exit { puts "Last exception was #{$!.inspect}" if $! } begin raise "Caught it" rescue RuntimeError end sleep 10 puts "Nothing happened" Note that both prog2 and prog3 terminate successfully and don't report any exception. prog4 reports Interrupt if you terminate with ^C. Your at_exit code can of course sit in a separate source file that you require. -- Posted via http://www.ruby-forum.com/.