首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从botocore捕获异常

如何从botocore捕获异常
EN

Stack Overflow用户
提问于 2017-06-29 14:36:18
回答 1查看 3.1K关注 0票数 3

在使用boto3Amazon SNS时,我得到了以下错误。我只想抓InvalidParameterException,怎么做呢?

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "D:\Logger\Notification.py", line 279, in <module>
    Push.subscribe(token1, 'android')
  File "D:\Logger\Notification.py", line 119, in subscribe
    'Enabled': b'True'
  File "C:\Python27\lib\site-packages\botocore\client.py", line 310, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Python27\lib\site-packages\botocore\client.py", line 599, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidParameterException: An error occurred (InvalidParameter) when calling the CreatePlatformEndpoint operation: Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-west-2:252285631092:endpoint/GCM/Test/06c4448e-545b-312a-978f-98af5d5829e4 already exists with the same Token, but different attributes.

如果我想抓住InvalidParameterException,它会显示

代码语言:javascript
运行
复制
NameError: global name 'InvalidParameterException' is not defined

我进口了botocore。现在,如果我试图抓住botorcore.errorfactory.InvalidParameterException,它会显示出来。

代码语言:javascript
运行
复制
AttributeError: 'module' object has no attribute 'InvalidParameterException'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-29 14:47:31

botocore库从一个基类中生成几个异常。抓住那个贱人:

代码语言:javascript
运行
复制
from botocore.exceptions import ClientError

try:    
    ...
except ClientError as e:
    ...

检查ClientError().response['Error']['Code']以改变您处理异常的方式,如果您想忽略特定的错误类型,则再次检查raise。见 documentation

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44828156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档