How to properly ignore exceptions

前端 未结 11 2143
粉色の甜心
粉色の甜心 2020-11-22 02:18

When you just want to do a try-except without handling the exception, how do you do it in Python?

Is the following the right way to do it?

try:
    s         


        
11条回答
  •  臣服心动
    2020-11-22 02:55

    How to properly ignore Exceptions?

    There are several ways of doing this.

    However, the choice of example has a simple solution that does not cover the general case.

    Specific to the example:

    Instead of

    try:
        shutil.rmtree(path)
    except:
        pass
    

    Do this:

    shutil.rmtree(path, ignore_errors=True)
    

    This is an argument specific to shutil.rmtree. You can see the help on it by doing the following, and you'll see it can also allow for functionality on errors as well.

    >>> import shutil
    >>> help(shutil.rmtree)
    

    Since this only covers the narrow case of the example, I'll further demonstrate how to handle this if those keyword arguments didn't exist.

    General approach

    Since the above only covers the narrow case of the example, I'll further demonstrate how to handle this if those keyword arguments didn't exist.

    New in Python 3.4:

    You can import the suppress context manager:

    from contextlib import suppress
    

    But only suppress the most specific exception:

    with suppress(FileNotFoundError):
        shutil.rmtree(path)
    

    You will silently ignore a FileNotFoundError:

    >>> with suppress(FileNotFoundError):
    ...     shutil.rmtree('bajkjbkdlsjfljsf')
    ... 
    >>> 
    

    From the docs:

    As with any other mechanism that completely suppresses exceptions, this context manager should be used only to cover very specific errors where silently continuing with program execution is known to be the right thing to do.

    Note that suppress and FileNotFoundError are only available in Python 3.

    If you want your code to work in Python 2 as well, see the next section:

    Python 2 & 3:

    When you just want to do a try/except without handling the exception, how do you do it in Python?

    Is the following the right way to do it?

    try :
        shutil.rmtree ( path )
    except :
        pass
    

    For Python 2 compatible code, pass is the correct way to have a statement that's a no-op. But when you do a bare except:, that's the same as doing except BaseException: which includes GeneratorExit, KeyboardInterrupt, and SystemExit, and in general, you don't want to catch those things.

    In fact, you should be as specific in naming the exception as you can.

    Here's part of the Python (2) exception hierarchy, and as you can see, if you catch more general Exceptions, you can hide problems you did not expect:

    BaseException
     +-- SystemExit
     +-- KeyboardInterrupt
     +-- GeneratorExit
     +-- Exception
          +-- StopIteration
          +-- StandardError
          |    +-- BufferError
          |    +-- ArithmeticError
          |    |    +-- FloatingPointError
          |    |    +-- OverflowError
          |    |    +-- ZeroDivisionError
          |    +-- AssertionError
          |    +-- AttributeError
          |    +-- EnvironmentError
          |    |    +-- IOError
          |    |    +-- OSError
          |    |         +-- WindowsError (Windows)
          |    |         +-- VMSError (VMS)
          |    +-- EOFError
    ... and so on
    

    You probably want to catch an OSError here, and maybe the exception you don't care about is if there is no directory.

    We can get that specific error number from the errno library, and reraise if we don't have that:

    import errno
    
    try:
        shutil.rmtree(path)
    except OSError as error:
        if error.errno == errno.ENOENT: # no such file or directory
            pass
        else: # we had an OSError we didn't expect, so reraise it
            raise 
    

    Note, a bare raise raises the original exception, which is probably what you want in this case. Written more concisely, as we don't really need to explicitly pass with code in the exception handling:

    try:
        shutil.rmtree(path)
    except OSError as error:
        if error.errno != errno.ENOENT: # no such file or directory
            raise 
    

提交回复
热议问题