{"id":221,"hash":"1a01a0f730aa21b894770eb3b912f573eb0592fa032818eab8b7cd8299d47829","pattern":"How can I catch multiple exceptions in one line? (in the &quot;except&quot; block)","full_message":"I know that I can do:\n\ntry:\n    # do something that may fail\nexcept:\n    # do this if ANYTHING goes wrong\n\nI can also do this:\n\ntry:\n    # do something that may fail\nexcept IDontLikeYouException:\n    # say please\nexcept YouAreTooShortException:\n    # stand on a ladder\n\nBut if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this:\n\ntry:\n    # do something that may fail\nexcept IDontLikeYouException:\n    # say please\nexcept YouAreBeingMeanException:\n    # say please\n\nIs there a way that I can do something like this (since the action to take in both exceptions is to say please):\n\ntry:\n    # do something that may fail\nexcept IDontLikeYouException, YouAreBeingMeanException:\n    # say please\n\nNow this really won't work, as it matches the syntax for:\n\ntry:\n    # do something that may fail\nexcept Exception, e:\n    # say please\n\nSo, my effort to catch the two distinct exceptions doesn't exactly come through.\n\nIs there a way to do this?","ecosystem":"pypi","package_name":"exception","package_version":null,"solution":"From Python Documentation:\n\n  An except clause may name multiple exceptions as a parenthesized tuple, for example\n\nexcept (IDontLikeYouException, YouAreBeingMeanException) as e:\n    pass\n\nOr, for Python 2 only:\n\nexcept (IDontLikeYouException, YouAreBeingMeanException), e:\n    pass\n\nSeparating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now deprecated and does not work in Python 3; now you should be using as.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/6470428/how-can-i-catch-multiple-exceptions-in-one-line-in-the-except-block","votes":3965,"created_at":"2026-04-19T04:41:34.549790+00:00","updated_at":"2026-04-19T04:51:46.372253+00:00"}