Custom exception ใช้ super().__init__() และเก็บ object ไว้ใน attribute
ตัวอย่าง
# foobar.py
class FooException(Exception):
def __init__(self, text, *args):
super(FooException, self).__init__(text, *args)
self.text = text
try:
bar = input("Enter a string:")
if not isinstance(bar, basestring):
raise FooException(bar)
except FooException as r:
print('there is an error')
else:
print(type(bar))
print(bar)
รัน: $ python foobar.py Input: 'voila'
Enter a string:'voila'Voila