以下表格内容参考自:https://fishc.com.cn/thread-45814-1-1.html
1.异常类型:
AssertionError | 断言语句(assert)失败 |
AttributeError | 尝试访问未知的对象属性 |
EOFError | 用户输入文件末尾标志EOF(Ctrl+d) |
FloatingPointError | 浮点计算错误 |
GeneratorExit | generator.close()方法被调用的时候 |
ImportError | 导入模块失败的时候 |
IndexError | 索引超出序列范围 |
KeyError | 字典中查找的关键字不存在 |
KeyboardInterrupt | 用户输入中断键(Ctrl+c) |
MemoryError | 内存溢出(可通过删除对象释放内存) |
NameError | 尝试访问一个不存在的变量 |
NotImplementedError | 尚未实现的方法 |
OSError | 操作系统产生的异常(例如打开一个不存在的文件) |
OverflowError | 数值运算超出最大限制 |
ReferenceError | 弱引用(weak reference)试图访问一个已经被垃圾回收机制回收了的对象 |
RuntimeError | 运行时错误 |
StopIteration | 迭代器没有更多的值 |
SyntaxError | python语法错误 |
IndentationError | 缩进错误 |
TabError | Tab和空格混合使用 |
SystemError | python编译器系统错误 |
SystemExit | python编译器进程被关闭 |
TypeError | 不同类型间的操作无效 |
UnboundLocalError | 访问一个未初始化的本地变量(NameError的子类) |
UnicodeError | Unicode相关的错误(ValueError的子类) |
UnicodeEncodeError | Unicode编码时的错误(UnicodeError的子类) |
UnicodeDecodeError | Unicode解码时的错误(UnicodeError的子类 |
UnicodeTranslateError | Unicode转换时的错误(UnicodeError的子类) |
ValueError | 传入无效的参数 |
ZeroDivisionError | 除数为0 |
2.异常类的层次结构:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
+-- 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
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning
简单举例说明几个常见异常:
>>> #AsseError:assert断言语句,当assert后面的表达式为假时抛出异常
>>> assert 3>2
>>> assert 3<@
SyntaxError: invalid syntax
>>> assert 3<2
Traceback (most recent call last):
File "", line 1, in
assert 3<2
AssertionError
>>>
>>> #AttributeError:尝试访问对象不存在的属性或方法
>>> list1=[2,1,3,5,4]
>>> list1.sorted() #sorted()时python的内置函数,列表的排序函数时sort()
Traceback (most recent call last):
File "", line 1, in
list1.sorted()
AttributeError: 'list' object has no attribute 'sorted'
>>> list1.sort()
>>> list1
[1, 2, 3, 4, 5]
>>>
>>> #IndexError:超出序列索引范围
>>> list1=[2,1,2]
>>> list1[4]
Traceback (most recent call last):
File "", line 1, in
list1[4]
IndexError: list index out of range
>>>
>>> #KeyError:在字典中查找不存在的关键字
>>> dictinary={'A':'Ammy','B':'Binary','C':'Cindy'}
>>> dictinary['D']
Traceback (most recent call last):
File "", line 1, in
dictinary['D']
KeyError: 'D'
>>>
>>> #NameError:尝试访问一个不存在的变量
>>> a
Traceback (most recent call last):
File "", line 1, in
a
NameError: name 'a' is not defined
>>>
>>> #OSError:操作系统产生的异常,例如打开一个不存在的文件
>>> file=open(r'E:\helloWorld.txt','r')
Traceback (most recent call last):
File "", line 1, in
file=open(r'E:\helloWorld.txt','r')
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\helloWorld.txt'
>>>
>>> #TyprError:类型错误
>>> 1+'2'
Traceback (most recent call last):
File "", line 1, in
1+'2'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
>>> #ZeroDivisionError:除数为0
>>> 1/0
Traceback (most recent call last):
File "", line 1, in
1/0
ZeroDivisionError: division by zero
注:使用raise可以使代码自动引发一个异常
>>> raise TypeError #自动引发一个异常
Traceback (most recent call last):
File "", line 1, in
raise TypeError
TypeError