Custom attribute of custom exception:
class MyException(Exception):
def __init__(self, message):
self.msg = message
def __str__(self):
return repr(self.msg)
Exception re-raising
- best approach is to preserve the catched exception, add somehow the custom informations and reraise it.
- Good explanation with all (good and bad) possibilities: https://gist.github.com/bstpierre/148135#file-cleanup_and_reraise-py
Simple solution:
from sys import exc_info
def reraise(e, additional_message):
raise type(e), type(e)("%s \n CLARIFICATION: %s" % (e.args[1], additional_message)), exc_info()[2]
#enddef
# example call
try:
50 / 0
except ZeroDivisionError as e:
reraise(e, "Division by zero is not allowed!")