Exception
1.assertionerror举例
1 >>> my_list=[‘小甲鱼是帅哥‘] 2 >>> assert len(my_list)>0 3 >>> my_list.pop() 4 ‘小甲鱼是帅哥‘ 5 >>> assert len(my_list)>0 6 Traceback (most recent call last): 7 File "<pyshell#6>", line 1, in <module> 8 assert len(my_list)>0 9 AssertionError 10 >>>
2.attribute举例
1 >>> my_list.fishc #实际上my_list这个对象并没有fishc这样的属性,所以会报错 2 Traceback (most recent call last): 3 File "<pyshell#7>", line 1, in <module> 4 my_list.fishc 5 AttributeError: ‘list‘ object has no attribute ‘fishc‘
3.indexerror举例
1 >>> my_list=[1,2,3] 2 >>> my_list[3] 3 Traceback (most recent call last): 4 File "<pyshell#9>", line 1, in <module> 5 my_list[3] 6 IndexError: list index out of range
4.keyerror举例
1 >>> my_dict={‘one‘:1,‘two‘:2,‘three‘:3} 2 >>> my_dict[‘one‘] 3 1 4 >>> my_dict[‘four‘] #访问的字典元素不存在 5 Traceback (most recent call last): 6 File "<pyshell#12>", line 1, in <module> 7 my_dict[‘four‘] 8 KeyError: ‘four‘ 9 >>> my_dict.get(‘four‘) #使用get方法可以避免报错的出现,以避免让用户看到报错
5.nameerror举例
1 >>> fishc #尝试访问一个不存在的变量 2 Traceback (most recent call last): 3 File "<pyshell#14>", line 1, in <module> 4 fishc 5 NameError: name ‘fishc‘ is not defined
6.typeerror举例(不同类型间的无效操作)
1 >>> 1+‘1‘ #‘1’ 2 Traceback (most recent call last): 3 File "<pyshell#17>", line 1, in <module> 4 1+‘1‘ 5 TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘
7.zerodivisionerror举例
1 >>> 5/0 2 Traceback (most recent call last): 3 File "<pyshell#18>", line 1, in <module> 4 5/0 5 ZeroDivisionError: division by zero
原文:http://www.cnblogs.com/zhuzhubaoya/p/6536316.html