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

วิธี Handle Exception ที่โยนจาก Except Clause ใน Python

ถ้า code ใน except clause เอง raise exception ใช้ nested try-except

ตัวอย่าง

import sys

try:
    a = john  # NameError
except:
    try:
        4/0  # ZeroDivisionError
    except:
        print(sys.exc_info())

ผลลัพธ์

(, ZeroDivisionError('integer division or modulo by zero',), )

Exception ตัวที่สอง (ZeroDivisionError) ถูก catch แทนตัวแรก