| 
 | 
 
 
 楼主 |
发表于 2017-9-3 08:24:05
|
显示全部楼层
 
 
 
032异常处理:你不可能总是对的
 本帖最后由 摆渡终极鉴黄师 于 2017-9-3 11:24 编辑  
 
AssertionError                断言语句(assert)失败,例如 
>>> my_list = ['小甲鱼是不是帅哥'] 
>>> assert len(my_list) > 0 
>>> my_list.pop() 
'小甲鱼是不是帅哥' 
>>> assert len(my_list) > 0 
Traceback (most recent call last): 
  File "<pyshell#3>", line 1, in <module> 
    assert len(my_list) > 0 
AssertionError 
>>>  
 
AttributeError                尝试访问未知的对象属性,例如 
>>> my_list.fishc 
Traceback (most recent call last): 
  File "<pyshell#8>", line 1, in <module> 
    my_list.fishc 
AttributeError: 'list' object has no attribute 'fishc' 
>>>  
 
EOFError                        input()读取到EOF却没有接受任何数据,例如 
>>> my_list = [1, 2, 3] 
>>> my_list[3] 
Traceback (most recent call last): 
  File "<pyshell#10>", line 1, in <module> 
    my_list[3] 
IndexError: list index out of range 
>>>  
 
FloatingPointError        浮点计算错误 
 
GeneratorExit                generator.close()方法被调用的时候 
 
ImportError                导入模块失败的时候 
 
IndexError                索引超出序列的范围 
 
KeyError                        字典中查找一个不存在的关键字,例如 
>>> my_list[2] 
3 
>>> my_dict = {'one':1, 'two':2, 'three':3} 
>>> my_dict['one'] 
1 
>>> my_dict['four'] 
Traceback (most recent call last): 
  File "<pyshell#5>", line 1, in <module> 
    my_dict['four'] 
KeyError: 'four' 
>>>  
 
KeyboardInterrupt        用户输入中断键(Ctrl+c) 
 
MemoryError                内存溢出(可通过删除对象释放内存) 
 
NameError                尝试访问一个不存在的变量,例如 
 
NotImplementedError尚未实现的方法 
OSError                        操作系统产生的一场(例如打开一个不存在的文件) 
OverflowError                数值运算超出最大限制 
ReferenceError        弱引用(weak reference)试图访问一个已经被垃圾机制回收了的对象 
RuntimeError                一般的运行时错误 
StopIteration                迭代器没有更多的值 
SyntaxError                python的语法错误,例如 
>>> print 'I love FishC.com' 
SyntaxError: Missing parentheses in call to 'print' 
>>>  
 
IndentationError        缩进错误 
TabError                        Tab和空格混合使用 
SystemError                Python编译器系统错误 
SystemExit                python编译器进程被关闭 
TypeError                不同类型间的无效操作 
UnboundLocalError        访问一个未初始化的本地变量(NameError的子类) 
UnicodeError                Unicode相关的错误(ValueError的子类) 
UnicodeEncodeError Unicode编码时的错误(UnicodeError的子类) 
UnicodeDecodeError Unicode解码时的错误(UnicodeError的子类) 
UnicodeTranslateError Unicode转换时的错误(UnicodeError的子类) 
ValueError                传入无效的参数 
ZeroDivisionError        除数为零,例如 
>>> 5/0 
Traceback (most recent call last): 
  File "<pyshell#10>", line 1, in <module> 
    5/0 
ZeroDivisionError: division by zero 
>>>  |   
 
 
 
 |