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

Cách handle exception được throw bởi except clause trong Python?

Chúng ta có scenario mà code trong except clause tự raise exception. Trong code cho trước, chúng ta có thể handle exception được raise bởi except clause như sau.

Ví dụ

import sys
try:
    a = john
except:
    try:
        4/0
    except:
        print(sys.exc_info())

Kết quả

Chúng ta nhận được output sau:

"C:/Users/TutorialsPoint1/~.py"
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x0000000002BD4B88>)