5. 内置异常

异常

描述

AssertionError

assert语句触发

AttributeError

访问了不存在的属性

EOFError

文件读取结束触发

FloatingPointError

浮点操作失败

GeneratorExit

生成器退出

ImportError

导入错误

ModuleNotFoundError

模块没有找到

IndexError

使用了不存在的索引

KeyError

使用了不存在的key

MemoryError

内存错误

NotImplementedError

方法没有实现

OSError

操作系统错误

OverflowError

内存溢出错误

RecursionError

递归错误

ReferenceError

引用错误

RuntimeError

运行时错误

StopIteration

停止迭代触发

StopAsyncIteration

停止异步迭代触发

SyntaxError

语法错误触发

IndentationError

缩进不正确

TabError

即使用table有使用空格导致的

SystemError

系统错误

SystemExit

使用sys.exit()

TypeError

类型错误

UnboundLocalError

未绑定的本地变量

UnicodeError

编码解码错误

UnicodeEncodeError

编码错误

UnicodeDecodeError

解码错误

UnicodeTranslateError

unicode转化错误

ValueError

值错误

ZeroDivisionError

除0错误

EnvironmentError

环境错误

IOError

io错误

异常类关系图

BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
    +-- StopIteration
    +-- StopAsyncIteration
    +-- ArithmeticError
    |    +-- FloatingPointError
    |    +-- OverflowError
    |    +-- ZeroDivisionError
    +-- AssertionError
    +-- AttributeError
    +-- BufferError
    +-- EOFError
    +-- ImportError
    |    +-- ModuleNotFoundError
    +-- LookupError
    |    +-- IndexError
    |    +-- KeyError
    +-- MemoryError
    +-- NameError
    |    +-- UnboundLocalError
    +-- OSError
    |    +-- BlockingIOError
    |    +-- ChildProcessError
    |    +-- ConnectionError
    |    |    +-- BrokenPipeError
    |    |    +-- ConnectionAbortedError
    |    |    +-- ConnectionRefusedError
    |    |    +-- ConnectionResetError
    |    +-- FileExistsError
    |    +-- FileNotFoundError
    |    +-- InterruptedError
    |    +-- IsADirectoryError
    |    +-- NotADirectoryError
    |    +-- PermissionError
    |    +-- ProcessLookupError
    |    +-- TimeoutError
    +-- ReferenceError
    +-- RuntimeError
    |    +-- NotImplementedError
    |    +-- RecursionError
    +-- SyntaxError
    |    +-- IndentationError
    |         +-- TabError
    +-- SystemError
    +-- TypeError
    +-- ValueError
    |    +-- UnicodeError
    |         +-- UnicodeDecodeError
    |         +-- UnicodeEncodeError
    |         +-- UnicodeTranslateError
    +-- Warning
        +-- DeprecationWarning
        +-- PendingDeprecationWarning
        +-- RuntimeWarning
        +-- SyntaxWarning
        +-- UserWarning
        +-- FutureWarning
        +-- ImportWarning
        +-- UnicodeWarning
        +-- BytesWarning
        +-- ResourceWarning