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

วิธี Raise Exception ใน Except Block และ Catch ใน Except Block ต่อไป Python

ใน try block เดียว มีเพียงหนึ่ง except clause ถูก invoke เท่านั้น ต้องใช้ nested try blocks

ตัวอย่าง

try:
    try:
        1/0
    except ArithmeticError as e:
        if str(e) == "Zero division":
            print("thumbs up")
        else:
            raise
except Exception as err:
    print("thumbs down")
    raise err

ผลลัพธ์

thumbs down
Traceback (most recent call last):
  File "...", line 11, in 
    raise err
  File "...", line 3, in 
    1/0
ZeroDivisionError: division by zero