boto3 exception handling

Note that when using boto3, I checked when I wanted to handle errors when reading a non-existent S3 object.

A NoSuchKey error is returned, but it seems to be a dynamically generated exception class and cannot be imported and described in the except clause.

The correspondence method was written in stackoverflow. http://stackoverflow.com/questions/33068055/boto3-python-and-how-to-handle-errors

The following is an excerpt.

import boto3
from botocore.exceptions import ClientError

try:
    iam = boto3.client('iam')
    user = iam.create_user(UserName='fred')
    print "Created user: %s" % user
except ClientError as e:
    if e.response['Error']['Code'] == 'EntityAlreadyExists':
        print "User already exists"
    else:
        print "Unexpected error: %s" % e

Catch the exception as ClientError of botocore.exceptions and check the error content with the value of response ['Error'] ['Code'].

Recommended Posts

boto3 exception handling
Exception handling
Python exception handling
Python exception handling
Python, about exception handling
Fizzbuzz with exception handling
Python exception handling (Python learning memo ⑥)
Exception handling during Python API communication
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to cx_Oracle] (12th) DB Exception Handling
Pharmaceutical company researchers summarized Python exception handling
Data handling
Python exception handling a little more convenient
Module import and exception handling in python