David Aldrich wrote: > Consider a 'send' method that sends a message to another system via a > socket. This method will wait for a response before returning. There are > two possible error conditions: > > > 1) Timeout - i.e. no response received > > 2) Illegal response received > > I need to communicate these errors to the caller of send(). So far I have > just raised a RuntimeError exception for both errors, and stated what > happened like this: > > raise RuntimeError("Message timeout") > > That's fine if the caller just wants to print the error but not so good if > the code needs to act differently according to which error condition > occurred. > > So, my question is, what's the pythonic way of doing this? Should I > subclass RuntimeError for each possible error condition? E.g.: > > class MessageTimeoutError(RuntimeError): pass > class IllegalResponseError(RuntimeError): pass
If you don't want to let the original timeout error bubble up you can create your own little hierarchy of exceptions: class ResponseError(Exception): pass class TimeoutError(ResponseError): pass class BadDataError(ResponseError): pass Then the baseclass of ResponseError doesn't matter much as client code that wants to catch every expected error can catch ResponseError. You can later add subclasses as needed without breaking this catch-all client. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor