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

วิธี Handle Python Exception ภายใน If Statement

วาง try-except ครอบ if-else

ตัวอย่าง

a, b = 5, 0
try:
    if b != 0:
        print(a/b)
    else:
        a/b
        raise ZeroDivisionError
except Exception as e:
    print(e)

ผลลัพธ์

integer division or modulo by zero