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

วิธี Catch IndexError Exception ใน Python

IndexError raised เมื่อ sequence index out of range

ตัวอย่าง

import sys

try:
    my_list = [3, 7, 9, 4, 6]
    print(my_list[6])  # IndexError
except IndexError as e:
    print(e)
    print(sys.exc_type)

ผลลัพธ์

list index out of range