How to properly ignore exceptions

前端 未结 11 2122
粉色の甜心
粉色の甜心 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:41
    try:
        doSomething()
    except: 
        pass
    

    or

    try:
        doSomething()
    except Exception: 
        pass
    

    The difference is that the first one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from exceptions.BaseException, not exceptions.Exception.

    See documentation for details:

    • try statement
    • exceptions
    0 讨论(0)
  • 2020-11-22 02:45

    I needed to ignore errors in multiple commands and fuckit did the trick

    import fuckit
    
    @fuckit
    def helper():
        print('before')
        1/0
        print('after1')
        1/0
        print('after2')
    
    helper()
    
    0 讨论(0)
  • 2020-11-22 02:50

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

    This will help you to print what the exception is:( i.e. try catch without handling the exception and print the exception.)

    import sys
    try:
        doSomething()
    except:
        print "Unexpected error:", sys.exc_info()[0]
    
    0 讨论(0)
  • 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 
    
    0 讨论(0)
  • 2020-11-22 02:58
    try:
          doSomething()
    except Exception: 
        pass
    else:
          stuffDoneIf()
          TryClauseSucceeds()
    

    FYI the else clause can go after all exceptions and will only be run if the code in the try doesn't cause an exception.

    0 讨论(0)
  • 2020-11-22 02:58

    I usually just do:

    try:
        doSomething()
    except:
        _ = ""
    
    0 讨论(0)
提交回复
热议问题