Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Quick confirmation on check for substring in Python 3.6

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading