Asked • 05/20/19

Catch multiple exceptions in one line (except block)?

I know that I can do: try: # do something that may fail except: # do this if ANYTHING goes wrongI can also do this: try: # do something that may fail except IDontLikeYouException: # say please except YouAreTooShortException: # stand on a ladderBut if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this: try: # do something that may fail except IDontLikeYouException: # say please except YouAreBeingMeanException: # say pleaseIs there any way that I can do something like this (since the action to take in both exceptions is to `say please`): try: # do something that may fail except IDontLikeYouException, YouAreBeingMeanException: # say pleaseNow this really won't work, as it matches the syntax for: try: # do something that may fail except Exception, e: # say pleaseSo, my effort to catch the two distinct exceptions doesn't exactly come through.Is there a way to do this?

1 Expert Answer

By:

Benjamin W. answered • 02/06/21

Tutor
New to Wyzant

Graduate Student and Python Developer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.