【Pyton】【小甲鱼】异常处理:你不可能总是对的

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

 ——————————————————————————————————————————————————————

1.try……except

1 try:
2     f=open('我为什么是一个文件.txt')
3     print(f.read())
4     f.close()
5 except OSError:
6     print('文件出错啦') #不会报错了,如果进入except就会打印出print('文件出错啦')的提示信息

如果开发人员还想看到报错那么完善为如下代码:

1 try:
2     f=open('我为什么是一个文件.txt')
3     print(f.read())
4     f.close()
5 except OSError as reason:
6     print('文件出错啦\n错误的原因是:'+str(reason))

捕获多个异常抛出except情况:

1 try:
2     sum=1+'1'
3     f=open('我为什么是一个文件.txt')
4     print(f.read())
5     f.close()
6 except OSError as reason:
7     print('文件出错啦\n错误的原因是:'+str(reason))
8 except TypeError as reason:
9     print('类型出错啦\n错误的原因是:'+str(reason))
——————————————————————————————————————————————————————————————
两个except可以优化为一个except如下:
except(OSError,TypeError):
    print('出错啦')

except不明确error报错:

1 try:
2     sum=1+'1'
3     f=open('我为什么是一个文件.txt')
4     print(f.read())
5     f.close()
6 except: #except后未明确指出进入哪个错误
7     print('出错啦')

2.finally:无论如何都会被执行的代码

可以把上方f.close()代码写到finally中,try中不管有什么报错,那么close()语句都会被执行。

3.raise:自己引出一个异常

 1 >>> raise
 2 Traceback (most recent call last):
 3   File "<pyshell#0>", line 1, in <module>
 4     raise
 5 RuntimeError: No active exception to reraise
 6 >>> 1/0
 7 Traceback (most recent call last):
 8   File "<pyshell#1>", line 1, in <module>
 9     1/0
10 ZeroDivisionError: division by zero
11 >>> raise ZeroDivisionError
12 Traceback (most recent call last):
13   File "<pyshell#2>", line 1, in <module>
14     raise ZeroDivisionError
15 ZeroDivisionError
16 >>> raise ZeroDivisionError('除数为零的异常')
17 Traceback (most recent call last):
18   File "<pyshell#3>", line 1, in <module>
19     raise ZeroDivisionError('除数为零的异常')
20 ZeroDivisionError: 除数为零的异常

 

posted @ 2017-03-11 21:45  猪猪宝丫  阅读(454)  评论(0编辑  收藏  举报