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

วิธี Catch KeyError Exception ใน Python

KeyError raised เมื่อ key ไม่อยู่ใน dictionary

ตัวอย่าง

import sys

try:
    s = {'a':5, 'b':7}['c']
except:
    print(sys.exc_info())

ผลลัพธ์

(, KeyError('c',), )