On Nov 8, 2007, at 3:04 PM, Alex D. wrote:
I’m write a wrapper for a web service and am wondering about the best
way to handle exceptions.
The main method in the wrapper sends either a HEAD or GET request to a
web server using Net::HTTP.
Should I catch any exceptions thrown by Net::HTTP and then throw back
my own exception, or should I leave the exceptions totally un-handled?
i generally do one of two things
a) let the exception pass through. this is ok if, and only if, there
is nothing reasonable your code could do to retry. if you do retry
you have you have a configurable numbmer of retries, of course.
b) wrap the errors. my preferred approach is something like
module Wrapper
class Error < ::StandardError
attr_accessor :exception
def self.wrapping exception, *a, &b
e = new *a, &b
e.exception = exception
e
end
end
class Specific < Error; end
class EvenMoreSpecific < Specific; end
end
this allows client code respond appropriately to specific errors or
simply to rescue the entire class of errors your wrapper might throw
and, when you want to wrap another error you can
rescue => e
raise Error.wrapping(e)
end
i would say that you have to have a good reason to pick b over a, but
they do exist.
cheers.
a @ http://codeforpeople.com/