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

วิธี Catch TypeError Exception ใน Python

TypeError raised เมื่อ combine object type ผิด หรือ call function ด้วย argument type ผิด

ตัวอย่าง

import sys

try:
    ny = 'Statue of Liberty'
    my_list = [3, 4, 5, 8, 9]
    print(my_list + ny)  # TypeError
except TypeError as e:
    print(e)
    print(sys.exc_type)

ผลลัพธ์

can only concatenate list (not "str") to list