Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Pass Variable ให้ Exception ใน Python

สร้าง custom exception class มี __init__ รับ variable

ตัวอย่าง

class ExampleException(Exception):
    def __init__(self, foo):
        self.foo = foo

try:
    raise ExampleException("Bar!")
except ExampleException as e:
    print(e.foo)

ผลลัพธ์

Bar!