From: Martin Hansen on 5 Aug 2010 06:06 Hello, I am interested in abstracting exception handling so it can be included to any script from some external file. First, I want to setup signal handling, and last I want to deal with exceptions. Now, in this little test of mine something is bad, since the exception is not caught. I suspect something is going out of scope and lost? Martin #!/usr/bin/env ruby def do_at_begin exit_status = "OK" # Install signal handlers %w( INT TERM QUIT ).each do |signal| Signal.trap(signal) do exit_status = signal exit end end exit_status end def do_at_exit(exit_status="OK-default") begin rescue Exception => exception $stderr.puts "Exception caught!" $stderr.puts exit_status $stderr.puts exception.backtrace ensure puts "DEBUG EXIT STATUS: #{exit_status}" end end exit_status = do_at_begin raise END { do_at_exit(exit_status) } -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 5 Aug 2010 09:29 > I suspect something is going out of scope and lost? Nope. The exception is raised, nothing catches it, *then* your END block is executed. You can only catch exceptions which are raised after the 'begin' of the exception block. Depending on what you're trying to do, the at_exit function might help you. Otherwise, you need to restructure your code like this: begin run_my_program rescue Exception puts "I got #{$!}" end -- Posted via http://www.ruby-forum.com/.
From: Martin Hansen on 5 Aug 2010 09:51 I was hoping for a setup like this in two files: my_script.rb: include "my_class" <do_stuff> or raise __END__ my_class.rb: def do_at_begin set trap for signals. status end def do_at_exit begin rescue Exception ensure p status end end status = do_at_begin END { do_at_exit(status) } __END__ I want to keep the my_script.rb file minimal and hence move the error handling to the included file. I have no idea if this can be done? Martin -- Posted via http://www.ruby-forum.com/.
From: Andrew Wagner on 5 Aug 2010 10:02 [Note: parts of this message were removed to make it a legal post.] What kind of stuff are you doing in my_script.rb? Defining a class? A method? Several methods? Just evaluating code? You might be able to use hook methods to wrap new code that gets defined, but you're being pretty vague right now. On Thu, Aug 5, 2010 at 9:51 AM, Martin Hansen <mail(a)maasha.dk> wrote: > I was hoping for a setup like this in two files: > > my_script.rb: > > include "my_class" > > <do_stuff> or raise > > __END__ > > > my_class.rb: > > def do_at_begin > set trap for signals. > status > end > > def do_at_exit > begin > rescue Exception > ensure > p status > end > end > > status = do_at_begin > > END { > do_at_exit(status) > } > > __END__ > > I want to keep the my_script.rb file minimal and hence move the error > handling to the included file. I have no idea if this can be done? > > > Martin > -- > Posted via http://www.ruby-forum.com/. > >
From: Martin Hansen on 5 Aug 2010 10:55
Andrew Wagner wrote: > What kind of stuff are you doing in my_script.rb? Defining a class? A > method? Several methods? Just evaluating code? <do_stuff> would be something simple like parsing a tabular file and raising an error if the format is wrong. Cheers, Martin -- Posted via http://www.ruby-forum.com/. |