0

While executing a try except process, I get different types of exceptions, there is one exactly that I want the process to act different than with the others.

This is my process, as simple as that:

try:
    variable = do_something()

except Exception as e:
    print(e)
    continue

It catches different types of exceptions during the process, but the exact exception I want to address differently is:

Message: invalid session id
Stacktrace:
#0 0x56107b4a8e89 <unknown>

I am thinking on addressing the following way:

if 'invalid session id' in str(e):
            print('passing')
            pass

But I would like to know if there is a more elegant of pythonic way to do it

The Dan
  • 1,408
  • 6
  • 16
  • 41
  • 1
    Does this answer your question? [Python: One Try Multiple Except](https://stackoverflow.com/questions/6095717/python-one-try-multiple-except) – Kemp May 20 '21 at 13:00

1 Answers1

1

Yes you can catch different types of exceptions by using the classes

import sys

try:
    # Do something
except EOFError as e:
    print(e)
except AnotherError as f:
    print(f)
excepe Exception as g:
    #Reach here if no specific exception was caught
    print(g)
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26