$ ls ~yifei/notes/

Python 中的异常总结

Posted on:

Last modified:

User defined class

For user defined exceptions, just subclass Exception

Catching Exceptions

except Exception as e:  # SystemExit, KeyboardInterrupt, GeneratorExit is not captured
    log("Reason", e)    # the baseline is to record

容易抛出异常的地方

  • 网络读取
  • 调用其他命令超时
  • 读取文件等数据

内置的异常树

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

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.

友情链接: MySQL 教程站