Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách catch TypeError Exception trong Python?

TypeError được gây ra bởi việc combine wrong type của objects, hoặc gọi function với wrong type của object.

Ví dụ

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

Kết quả

can only concatenate list (not "str") to list
<type 'exceptions.TypeError'>