I just want to make sure I’m doing this in the proper “pythonic” way – I want to make sure I’ve defind – and am using – this exception class correctly. Especially the eval(repr()) logic – it’s mostly for cleanliness, I understand why you end up with quotes around the string repr() returns, but I don’t like to log them.
class IPCClientError(Exception):
""" General IPC Client Exception class """
def __init__(self, value = "Unspecified error"):
self.val = value + ", see IPC client log for details."
def __str__(self):
return eval(repr(self.val))
When I raise the exception, I use something like:
raise IPCClientError("Socket error")
And then the calling method will have something like this:
except IPCClientError, exc:
self.log.error(str(exc))
return ERROR
Eek, what are you trying to accomplish here? isn’t
self.valalready supposed to be a string?The way to avoid the quotes that
reprattaches is not to use it in the first place.If you’re worried that the value passed to the constructor won’t be a string, well – that will fail in the constructor (
unicodequibbles notwithstanding) anyway and you’ll just get aTypeErrorraised before your custom exception can be.As for how you handle the exception, exception handling is kind of an art, and really not something that can be covered in this space…