如何在 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'>

更新於:2020 年 2 月 12 日

593 次觀看

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.