Catch multiple exceptions in one line (except block)

前端 未结 5 1991
故里飘歌
故里飘歌 2020-11-22 03:41

I know that I can do:

try:
    # do something that may fail
except:
    # do this if ANYTHING goes wrong

I can also do this:



        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 04:02

    How do I catch multiple exceptions in one line (except block)

    Do this:

    try:
        may_raise_specific_errors():
    except (SpecificErrorOne, SpecificErrorTwo) as error:
        handle(error) # might log or have some other default behavior...
    

    The parentheses are required due to older syntax that used the commas to assign the error object to a name. The as keyword is used for the assignment. You can use any name for the error object, I prefer error personally.

    Best Practice

    To do this in a manner currently and forward compatible with Python, you need to separate the Exceptions with commas and wrap them with parentheses to differentiate from earlier syntax that assigned the exception instance to a variable name by following the Exception type to be caught with a comma.

    Here's an example of simple usage:

    import sys
    
    try:
        mainstuff()
    except (KeyboardInterrupt, EOFError): # the parens are necessary
        sys.exit(0)
    

    I'm specifying only these exceptions to avoid hiding bugs, which if I encounter I expect the full stack trace from.

    This is documented here: https://docs.python.org/tutorial/errors.html

    You can assign the exception to a variable, (e is common, but you might prefer a more verbose variable if you have long exception handling or your IDE only highlights selections larger than that, as mine does.) The instance has an args attribute. Here is an example:

    import sys
    
    try:
        mainstuff()
    except (KeyboardInterrupt, EOFError) as err: 
        print(err)
        print(err.args)
        sys.exit(0)
    

    Note that in Python 3, the err object falls out of scope when the except block is concluded.

    Deprecated

    You may see code that assigns the error with a comma. This usage, the only form available in Python 2.5 and earlier, is deprecated, and if you wish your code to be forward compatible in Python 3, you should update the syntax to use the new form:

    import sys
    
    try:
        mainstuff()
    except (KeyboardInterrupt, EOFError), err: # don't do this in Python 2.6+
        print err
        print err.args
        sys.exit(0)
    

    If you see the comma name assignment in your codebase, and you're using Python 2.5 or higher, switch to the new way of doing it so your code remains compatible when you upgrade.

    The suppress context manager

    The accepted answer is really 4 lines of code, minimum:

    try:
        do_something()
    except (IDontLikeYouException, YouAreBeingMeanException) as e:
        pass
    

    The try, except, pass lines can be handled in a single line with the suppress context manager, available in Python 3.4:

    from contextlib import suppress
    
    with suppress(IDontLikeYouException, YouAreBeingMeanException):
         do_something()
    

    So when you want to pass on certain exceptions, use suppress.

提交回复
热议问题