python - 捕获特定异常

标签 python

在我的函数中,如果 text_content 是例如德国人,功能失败。通过当前的解决方案,我得到了:

print(type(e))
>>> <class 'google.api_core.exceptions.InvalidArgument'>

我想更具体,所以改为 except Exception as e: except InvalidArgument as e: 但是,这不起作用。你知道这里出了什么问题吗?

text_content= "Trauben sind gut. Bananen sind schlecht."
try:
    sample_analyze_entity_sentiment(text_content)
except Exception as e:
    print(type(e))  

最佳答案

您需要导入 InvalidArgument 异常。

from google.api_core.exceptions import InvalidArgument

text_content= "Trauben sind gut. Bananen sind schlecht."
try:
    sample_analyze_entity_sentiment(text_content)
except InvalidArgument as e:
    print(type(e))

关于python - 捕获特定异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363804/

相关文章:

python - 集合列表的减法

Python 将 datetime.time 转换为箭头

python - matplotlib opencv 图像子图

python - 暂停和恢复功能

python - Python 中的 3D BPP 算法

python - 列出扩展的奇怪行为

python - 在最近的日期时间/时间戳上合并数据帧

python - 在同一 iloc 语句中使用切片和 bool 索引

python - 向文章 django createviewforeignkey 添加评论

python - 最小化到 gnome 面板