如何在 Python 中捕捉 TypeError 異常?


TypeError 的成因是由於對錯誤型別的物件進行組合,或呼叫了具有錯誤型別的物件的函式。

示例

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

輸出

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

更新時間: 12-2 月-2020

591 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.