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
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
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.
Then when you could do is have a module which overrides Ruby’s method
that
gets called when you define a method. You can alias the method being
created
to something else, then define a new method which calls that one, but
wraps
it in whatever error-handling code you want.
Sorry, I have no idea how to do that - it sounds terribly complicated.
Is there no simple solution? Could you perhaps give a small example?
Then when you could do is have a module which overrides Ruby’s method
that
gets called when you define a method. You can alias the method being
created
to something else, then define a new method which calls that one, but
wraps
it in whatever error-handling code you want.
original_method = "original #{method}"
alias_method original_method, method
define_method(method) do |*args|
begin
puts "gonna try something dangerous"
send original_method, *args
rescue Exception
ensure
puts "My last will and testament..."
end
end
@_adding_a_method = false
end
end
end
The idea is, when you include this module, then it watches for any new
methods you define. When you define one, it immediately hides it away,
and
creates a new method, which calls the original one, but inside a
begin/rescue/ensure block. The @_adding_a_method flag is just to keep
from
infinite recursion when you create the new method (because it will also
trigger method_added.
Now in your main class, do something like this:
include ErrorHandling
def do_something
puts “do something dangerous”
end
For a somewhat more robust ErrorHandling module, which requires 1.9, try
the
code below. I should also give credit to the ruby metaprogramming
screencasthttp://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming
for
inspiration for this code. It’s practically ripped straight out of
there,
except theirs does tracing, as opposed to just error-handling.
module ErrorHandling
def self.included(klass)
klass.instance_methods(false).each do |existing_method|
wrap(klass, existing_method)
end
def klass.method_added(method)
unless @watch_calls_internal
@watch_calls_internal = true
ErrorHandling.wrap(self, method)
@watch_calls_internal = false
end
end
end
def self.wrap(klass, method)
klass.class_eval do
name = method.to_s
original_method = instance_method(name)
define_method(name) do |*args, &block|
begin
puts "attempting..."
result = original_method.bind(self).call(*args, &block)
rescue Exception
ensure
puts "last will..."
end
result
end
end
I have been looking at your suggestions and done a few tests. However, I
wonder if there is not a more simple way - and especially since these
suggestions depends on methods being defined in order to invoke error
handling. Here is a real life example of the type of minimalistic
scripts I want to write:
bp.mktmpdir # create an optional temporary directory that is auto
deleted.
bp.each_record do |record|
do something to each record (possibly according to the options).
e.g.:
record[“SEQ”].upcase! if record.has_key? “SEQ”
bp.puts record
raise # Debug test of invokation of error handling
end
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), but I am not defining any methods and thus the suggested
meta-programming approaches seem to fall short.
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 <top (required)>' ./test.rb:3:inrequire’
./test.rb:3:in <main>' exit status: OK Hello World Last exception was #<RuntimeError: raise from main> ./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(
$! does not tell the difference between interrupt, terminate,
and quit (perhaps that could be fixed? - just a thought).
It does for me.
$ cat ert.rb
at_exit { puts “Last exception was #{$!.inspect}” if $!; exit! }
sleep 100
puts “Nothing happened”
Ctrl-C:
$ ruby ert.rb
^CLast exception was Interrupt
ert.rb:2:in `sleep’: Interrupt
from ert.rb:2
kill -TERM (from another window):
$ ruby ert.rb
Last exception was #<SignalException: SIGTERM>
Terminated
kill -QUIT:
Last exception was #<SignalException: SIGQUIT>
Quit
This is with ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux] under
Ubuntu Lucid amd64.
So I don’t understand why you’re trying to trap all of these signals,
when they all cause the program to terminate, and they can all be
distinguished in $!. But perhaps your platform behaves differently?
Final note: inside your at_exit handler, you can use ‘exit!’ to prevent
any further processing, i.e. the default output of the backtrace or
signal.