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

วิธี Catch LookupError Exception ใน Python

LookupError เป็น base class ของ IndexError และ KeyError

ตัวอย่าง IndexError

import sys

try:
    foo = ['a', 's', 'd', 'f', 'g']
    print(foo[5])  # IndexError
except IndexError as e:
    print(e)
    print(sys.exc_type)

ผลลัพธ์

list index out of range