Quick confirmation on check for substring in Python 3.6

Question:

I write my code in Python 3.8.9 with this line, which works:


try:
    ...
except Exception as e:
    if "AlreadyExistsException" in e:

When deploying it in a Python 3.6 environment, I get this error:

TypeError: argument of type 'AlreadyExistsException' is not iterable

Could someone help confirming that the operation to check for the existence of a substring AlreadyExistsException in the error string e like above does not work in Python 3.6? I don’t have Python 3.6 to test this out and too hesitated to install it to test this error. And if this is true, what is a workable way to check for substring in Python 3.6?

Asked By: Tristan Tran

||

Answers:

The correct way to check for a specific type of Exception would be:

try:
    ...
except AlreadyExistsException as e:
    # do something in response to this specific exception
    ...
except (SomeOtherException, AndAnotherException) as e:
    # do something in response to those specific exceptions
    ...
Answered By: Grismar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.