I've learned recently how to create custom exceptions in Python and implement them into a class. I'm trying to add an additional argument into my exception for more clarity, and can't seem to get the formatting done correctly.
Here's what I'm attempting:
class NonIntError(Exception):
pass
class intlist(List):
def __init__(self, lst = []):
for item in lst:
#throws error if list contains something other that int
if type(item) != int:
raise NonIntError(item, 'not an int')
else:
self.append(item)
Expected Results
il = intlist([1,2,3,'apple'])
>>> NonIntError: apple not an int
Results with error
il = intlist([1,2,3,'apple'])
>>> NonIntError: ('apple', 'not an int')
Restating my question, I would like know how to make my exception appear like the expected results.