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

อธิบาย Try Except Else Statement ใน Python

else clause หลัง except รันเมื่อ ไม่มี exception ใน try block

ตัวอย่าง

a = [11, 8, 9, 2]

try:
    foo = a[3]
except:
    print("index out of range")
else:
    print("index well within range")

ผลลัพธ์

index well within range

else รันเมื่อ control flow ออกจาก try block ปกติ (ไม่ใช่ exception, return, continue, break)