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

时间:2023-03-09 14:27:31
【Pyton】【小甲鱼】异常处理:你不可能总是对的

Exception

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

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

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

1.assertionerror举例

 >>> my_list=['小甲鱼是帅哥']
>>> assert len(my_list)>0
>>> my_list.pop()
'小甲鱼是帅哥'
>>> assert len(my_list)>0
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
assert len(my_list)>0
AssertionError
>>>

2.attribute举例

 >>> 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举例

 >>> my_list=[1,2,3]
>>> 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举例

 >>> my_dict={'one':1,'two':2,'three':3}
>>> my_dict['one']
1
>>> 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举例

 >>> 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’
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举例

 >>> 5/0
2 Traceback (most recent call last):
3 File "<pyshell#18>", line 1, in <module>
4 5/0
5 ZeroDivisionError: division by zero

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

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

1.try……except

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

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

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

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

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

except不明确error报错:

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

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

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

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

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