How Can I Find a List of All Exceptions That a Given Library Function Throws in Python?

前端 未结 4 1785
忘了有多久
忘了有多久 2021-01-11 10:16

Sorry for the long title, but it seems most descriptive for my question.

Basically, I\'m having a difficult time finding exception information in the official python

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 11:01

    Python doesn't have a mechanism right now for declaring which exceptions are thrown, unlike (for example) Java. (In Java you have to define exactly which exceptions are thrown by what, and if one of your utility methods needs to throw another exception then you need to add it to all of the methods which call it which gets boring quickly!)

    So if you want to discover exactly which exceptions are thrown by any given bit of python then you need to examine the documentation and the source.

    However python has a really good exception hierarchy.

    If you study the exception hierarchy below you'll see that the error superclass you want to catch is called StandardError - this should catch all the errors that might be generated in normal operations. Turning the error into into a string will give a reasonable idea to the user as to what went wrong, so I'd suggest your code above should look like

    from shutil import move
    try:
        move('somefile.txt', '/tmp/somefile.txt')
    except StandardError, e:
        print 'Move failed: %s' % e
    

    Exception hierarchy

    BaseException
    |---Exception
    |---|---StandardError
    |---|---|---ArithmeticError
    |---|---|---|---FloatingPointError
    |---|---|---|---OverflowError
    |---|---|---|---ZeroDivisionError
    |---|---|---AssertionError
    |---|---|---AttributeError
    |---|---|---BufferError
    |---|---|---EOFError
    |---|---|---EnvironmentError
    |---|---|---|---IOError
    |---|---|---|---OSError
    |---|---|---ImportError
    |---|---|---LookupError
    |---|---|---|---IndexError
    |---|---|---|---KeyError
    |---|---|---MemoryError
    |---|---|---NameError
    |---|---|---|---UnboundLocalError
    |---|---|---ReferenceError
    |---|---|---RuntimeError
    |---|---|---|---NotImplementedError
    |---|---|---SyntaxError
    |---|---|---|---IndentationError
    |---|---|---|---|---TabError
    |---|---|---SystemError
    |---|---|---TypeError
    |---|---|---ValueError
    |---|---|---|---UnicodeError
    |---|---|---|---|---UnicodeDecodeError
    |---|---|---|---|---UnicodeEncodeError
    |---|---|---|---|---UnicodeTranslateError
    |---|---StopIteration
    |---|---Warning
    |---|---|---BytesWarning
    |---|---|---DeprecationWarning
    |---|---|---FutureWarning
    |---|---|---ImportWarning
    |---|---|---PendingDeprecationWarning
    |---|---|---RuntimeWarning
    |---|---|---SyntaxWarning
    |---|---|---UnicodeWarning
    |---|---|---UserWarning
    |---GeneratorExit
    |---KeyboardInterrupt
    |---SystemExit
    

    This also means that when defining your own exceptions you should base them off StandardError not Exception.

    Base class for all standard Python exceptions that do not represent
    interpreter exiting.
    

提交回复
热议问题