Ở đây chúng ta pass variable cho exception cho trước. Chúng ta định nghĩa custom exception ExampleException là subclass của base class Exception và cũng định nghĩa method __init__. Chúng ta dùng try-except block để raise exception và pass variable cho exception như sau.
Ví dụ
class ExampleException(Exception):
def __init__(self, foo):
self.foo = foo
try:
raise ExampleException("Bar!")
except ExampleException as e:
print(e.foo)
Kết quả
"C:/Users/TutorialsPoint1/~bar.py" Bar!