Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách handle Python exception bên trong if statement?

Code có thể viết như sau để catch exception:

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

Chúng ta nhận được kết quả sau:

C:/Users/TutorialsPoint1/~.py integer division or modulo by zero