Exceptions can't be caught with try/except
See original GitHub issueWhenever I get an exception in boto which I want to catch, I copy and paste the exception code into the except
block, and it normally fails because python doesn’t know which exception I’m trying to catch.
Steps to reproduce
Run the following code:
import botocore
import boto3
client = boto3.client('s3')
b = 'somebucketnamewhichdoesntexist'
response = client.get_bucket_location(
Bucket=b
)
Because that bucket doesn’t exist, boto raises a botocore.errorfactory.NoSuchBucket
exception.
So copy and paste botocore.errorfactory.NoSuchBucket
into an except block.
import botocore
import boto3
client = boto3.client('s3')
b = 'somebucketnamewhichdoesntexist'
try:
response = client.get_bucket_location(
Bucket=b
)
except botocore.errorfactory.NoSuchBucket as e:
print('bucket %s doesnt exist')
Run this new code
Expected behaviour
- The
except
block catches the exception - The script prints
bucket somebucketnamewhichdoesntexist doesnt exist
- The script exits successfully
Actual Behaviour
The interpreter doesn’t know what botocore.errorfactory.NoSuchBucket
is
Traceback (most recent call last): File “botofail.py”, line 13, in <module> except botocore.errorfactory.NoSuchBucket as e: AttributeError: ‘module’ object has no attribute ‘NoSuchBucket’
Issue Analytics
- State:
- Created 6 years ago
- Reactions:30
- Comments:7 (1 by maintainers)
Top Results From Across the Web
8. Errors and Exceptions — Python 3.11.1 documentation
First, the try clause (the statement(s) between the try and except keywords) is executed. If no exception occurs, the except clause is skipped...
Read more >How to Catch Multiple Exceptions in Python - Rollbar
In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more...
Read more >Try, Except, else and Finally in Python - GeeksforGeeks
First try clause is executed i.e. the code between try and except clause. · If there is no exception, then only try clause...
Read more >python - Difference between except: and except Exception as e
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can ...
Read more >Try and Except in Python
Catching Exceptions in Python ... The try-except block can handle exceptions. This prevents abrupt exits of the program on error. In the example...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Modeled exceptions needs to be accessed through the client. So rather than having
botocore.errorfactory.NoSuchBucket
you needclient.exceptions.NoSuchBucket
. I agree that this is confusing and the current documentation is insufficient.More information on this can be found at this pr: https://github.com/boto/botocore/pull/1113 and this issue: https://github.com/boto/boto3/issues/167
I ran into the same issue, and what I want to do is debug why something isn’t working, but none of the standard python methods for try/except seem to work (still: may 2019).
botocore should remove the factories and modify them to drop static files into botocore so there was both traceability and ease of understanding. As it is, there are quite a few really poorly contrived python structures. The factories in use here are an anti-pattern for the user.