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