question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exceptions can't be caught with try/except

See original GitHub issue

Whenever 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:closed
  • Created 6 years ago
  • Reactions:30
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

105reactions
joguSDcommented, Jul 21, 2017

Modeled exceptions needs to be accessed through the client. So rather than having botocore.errorfactory.NoSuchBucket you need client.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

13reactions
brianbruggemancommented, May 24, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found