class My_custom_exception(Exception):
def __init__(self, ErrNum, *arg, **kwargs):
self.ErrNum = int(ErrNum)
self.ErrMsg = "" # Set appropriate message depending an error number
if self.ErrNum & 0x00000000 !=0:
self.ErrMsg = "NO ERROR"
if self.ErrNum & 0x00000001 !=0:
self.ErrMsg = "Error 1 occured"
if self.ErrNum & 0x00000002 !=0:
self.ErrMsg = "Error 2 occured"
if self.ErrNum & 0x00000004 !=0:
self.ErrMsg = "Error 3 occured"
if self.ErrNum & 0x00000008 !=0:
self.ErrMsg = "Error 4 occured"
def __str__(self):
return "ERROR (0x%08X): %s" % (self.ErrNum, self.ErrMsg )
def __repr__(self):
return self.__str__()
def check_error(error_nr):
if error_nr > 0:
raise My_custom_exception(error_nr)
try:
check_error(3)
except My_custom_exception as e:
print e
output:
My_custom_exception (0x00000003): Error 2 occurred.
My aim is to print both Error 1 occurred and Error 2 occurred.
Another problem if I give check_error(5) then Error 4 occurred, I want to avoid this as well.
self.ErrMsgrather than overwriting it).