[Tutor] Guidance on using custom exceptions please

David Aldrich David.Aldrich at EMEA.NEC.COM
Mon Oct 12 10:55:43 EDT 2015


Hi

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

Best regards

David



More information about the Tutor mailing list