马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
一、try—except—else try与else搭配,如果没检测出异常,它就会执行else语句的内容了。
try:
1/0
except:
print("逮到了~")
else:
print("没逮到~")
逮到了~
try:
1/1
except:
print("逮到了~")
else:
print("没逮到~")
1.0
没逮到~
二、try—except—finally 无论异常是否发生,都必须执行finally的内容
try:
1/0
except:
print("逮到了~")
else:
print("没逮到~")
finally:
print("逮没逮到都会吱一声~")
逮到了~
逮没逮到都会吱一声~
try: (finally通常用在执行收尾工作,比如关闭文件)
f=open("FishC.txt","w")
f.write("I love little sister.")
except:
print("出错了~")
finally:
f.close()
21
还有一种方法:try与finally直接搭配,把except省略掉:
try:
while True:
pass
finally:
print("晚安~") (跑起来后,发现这个程序它不动了,实际上它一直在跑,因为它触发了一个死循环,只有用ctrl+c把它停下来。)
晚安~
Traceback (most recent call last):
File "<pyshell#16>", line 2, in <module>
while True:
KeyboardInterrupt
语法总结:
语法一:
try:
检测范围
except [expression [as identifier]]:
异常处理代码
[except [expression [as identifier]]:
异常处理代码]*
[else:
没有触发异常时执行的代码]
[finally:
收尾工作执行的代码]
语法二:
try:
检测范围
finally:
收尾工作执行的代码
三、异常的嵌套
try:
try:
520+"FishC"
except:
print("内部异常!")
1/0
except:
print("外部异常!")
finally:
print("收尾工作~")
内部异常!
外部异常!
收尾工作~ (先执行嵌套的内部异常,再触发了外部异常,最后执行finally的收尾工作)
如果把外部错误1/0放到开头(内部异常的前面),看看怎么样:
try:
1/0
try:
520+"FishC"
except:
print("内部异常!")
except:
print("外部异常!")
finally:
print("收尾工作~")
外部异常! (内部异常被跳过了,连犯错的机会都没有了)
收尾工作~
前面发生的异常都是被动防御的姿态。
四、raise语句——自报异常
raise ValueError("值不正确。")
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
raise ValueError("值不正确。")
ValueError: 值不正确。
raise FishCError("小姐姐说你不行,你就不行~") (不能用raise语句去生成一个不存在的异常)
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
raise FishCError("小姐姐说你不行,你就不行~")
NameError: name 'FishCError' is not defined
五、raise语句还有偷天换日的功能
try:
1/0
except:
raise ValueError("这样可不行~")
Traceback (most recent call last):
File "<pyshell#40>", line 2, in <module>
1/0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#40>", line 4, in <module>
raise ValueError("这样可不行~")
ValueError: 这样可不行~ (ZeroDivisionError的异常被替换成了ValueError的异常)
六、异常链——raise后面加个from
raise ValueError("这样可不行~") from ZeroDivisionError
ZeroDivisionError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
raise ValueError("这样可不行~") from ZeroDivisionError
ValueError: 这样可不行~
七、assert语句——通常用于代码调试
s="FishC"
assert s == "FishC" (类似于if语句的判断,如果条件成立,没有反应)
assert s != "FishC"
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
assert s != "FishC"
AssertionError (不成立的话,会抛出AssertionError的异常,相当于你用这个,就不用去写if语句了,这个不等于怎么样,就怎么怎么样,直接用一个异常告诉你,这个与你的预期是不一样的,及时止损,及时进行一个调试)
八、利用异常来实现goto类似的功能
try:
while True:
while True:
for i in range(10):
if i > 3:
raise
print(i)
print("被跳过~")
print("被跳过~")
print("被跳过~")
except: (只要在try里面触发的异常,都会跳到except里面,相当于把它的目的放在except里面,“到这儿来了”,0,1,2,3到这儿来了。)
print("到这儿来了~")
0
1
2
3
到这儿来了~
|