Monday, March 9, 2009

Quickie for debugging

I find that I write scripts that have to absorb exceptions more often than I'd expect. This occasionally makes life difficult when I know there's an exception being raised, and I have some idea where, but not what it is. In the past, I'd use a tried and true instrumentation of the form:

begin
troubled_object.do_irritating_thing(:frequency => :irregularly)
rescue Object => ex
puts "#{ex.class}: #{ex.message}\n#{ex.backtrace.join($/)}"
end


Which is also the most concise way I know to generally evade Ruby's contraction of backtraces or the various backtrace trimming routines. I use it frequently enough that I keep a Kernel method snippet around for the odd times it gets too cumbersome.

But the other day it occurred to me, that especially for quick debugging there's an alternative approach:

troubled_objects_client.call_trouble rescue p $!