- #1 什么是异常?">#1 什么是异常?
- #2 异常的处理">#2 异常的处理
- #2.1
<font style="color:rgb(71, 101, 130);">try-except</font>">#2.1<font style="color:rgb(71, 101, 130);">try-except</font> - #2.2
<font style="color:rgb(71, 101, 130);">try-except-else</font>">#2.2<font style="color:rgb(71, 101, 130);">try-except-else</font> - #2.3
<font style="color:rgb(71, 101, 130);">try-except-else-finally</font>">#2.3<font style="color:rgb(71, 101, 130);">try-except-else-finally</font>
- #2.1
- #3 抛出异常">#3 抛出异常
- #4 总结">#4 总结
- #5 练习题">#5 练习题

- Python 基础教程

#1 什么是异常?
嫦娥本是后羿之妻,后羿射下 9 个太阳后西王母赐其不老仙药,但后羿不舍得吃下,就交于嫦娥保管。后羿门徒蓬蒙觊觎仙药,逼迫嫦娥交出仙药,嫦娥无奈情急之下吞下仙药,便向天上飞去。 在嫦娥奔月的这个故事中,后羿的门徒蓬蒙等人觊觎仙药,就是一个异常,因为他们本身的品性不好, 如果再吃了长生不老的仙药,会对世间造成很大的危害;嫦娥无奈之下吞下仙药,便是对异常情况的一个处理,为了让异常情况不产生负面影响所采取的措施。 在 Python 中,异常是在程序运行过程中发生的错误,当异常发生时,需要对异常进行处理,否则整个程序将崩溃。 举个例子:1
print(1 / 0)print("Done!")
2 例子中的
<font style="color:rgb(71, 101, 130);">1 / 0</font> 触发了 <font style="color:rgb(71, 101, 130);">ZeroDivisionError</font> 异常,由于没有对异常进行处理,导致了程序的崩溃,后面的语句没有再继续执行。
#2 异常的处理
当异常发生时,如果直接导致了程序的崩溃,不论对用户还是对开发者来说都是不友好的,这便要求我们在异常发生时,对异常进行处理。就像嫦娥一样,当发现品性不好的蓬蒙等人觊觎仙药时,为了阻止不好的情况发生,吞服了仙药,对异常进行了及时的处理。 下面,我们来看下如何进行异常的处理?#2.1 <font style="color:rgb(71, 101, 130);">try-except</font>
<font style="color:rgb(71, 101, 130);">try</font> 和 <font style="color:rgb(71, 101, 130);">except</font> 语句块可以用来捕获和处理异常,<font style="color:rgb(71, 101, 130);">try</font> 后面跟的是需要捕获异常的代码,<font style="color:rgb(71, 101, 130);">except</font> 后面跟的是捕获到异常后需要做的处理。每一个 <font style="color:rgb(71, 101, 130);">try</font> 语句块后面必须跟上一个 <font style="color:rgb(71, 101, 130);">except</font> 语句块,即使 <font style="color:rgb(71, 101, 130);">except</font> 语句块什么也不做。

1
try:print(1 / 0)except ZeroDivisionError:print("ZeroDivisionError happened!")print("Done!")
2
3
4
5
6 加了异常捕获和处理代码之后,当发生异常时,整个程序没有崩溃,而是继续执行后面的代码,所以输出了
<font style="color:rgb(71, 101, 130);">Done!</font>。大家要注意冒号及缩进哈!
<font style="color:rgb(71, 101, 130);">try</font> 语句块后面可以跟上多个 <font style="color:rgb(71, 101, 130);">except</font> 语句块。
1
try:print(1 / 0)#除0异常with open('test.log') as file:#文件不存在异常read_data = file.read()except ZeroDivisionError:print("ZeroDivisionError happened!")except FileNotFoundError:print("FileNotFoundError happened!")print("Done!")
2
3
4
5
6
7
8
9
10
11
12 在执行代码
<font style="color:rgb(71, 101, 130);">print(1 / 0)</font> 时,发生了除 0 异常,所以没有执行后面的打开文件的代码,后面的异常处理代码输出了 <font style="color:rgb(71, 101, 130);">ZeroDivisionError happened!</font>。
如果我们颠倒下代码的顺序:
1
try:with open('test.log') as file:#文件不存在异常read_data = file.read()print(1 / 0)#除0异常except ZeroDivisionError:print("ZeroDivisionError happened!")except FileNotFoundError:print("FileNotFoundError happened!")print("Done!")
2
3
4
5
6
7
8
9
10
11
12 代码颠倒之后,在执行打开文件的代码时,发生了文件不存在的异常,所以没有执行后面的代码,后面的异常处理代码输出了
<font style="color:rgb(71, 101, 130);">FileNotFoundError happened!</font>。
#2.2 <font style="color:rgb(71, 101, 130);">try-except-else</font>
<font style="color:rgb(71, 101, 130);">try-except</font> 语句块后面可以跟上 <font style="color:rgb(71, 101, 130);">else</font> 语句块,当没有异常发生时,会执行 <font style="color:rgb(71, 101, 130);">else</font> 语句块中的代码。

1
try:print(1 / 1)except ZeroDivisionError:print("ZeroDivisionError happened!")else:print("Exception not happened")print("Done!")
2
3
4
5
6
7
8 程序中没有发生异常,所以执行
<font style="color:rgb(71, 101, 130);">else</font> 语句块中的代码,输出了 <font style="color:rgb(71, 101, 130);">Exception not happened</font>。
#2.3 <font style="color:rgb(71, 101, 130);">try-except-else-finally</font>
<font style="color:rgb(71, 101, 130);">try-except-else</font> 语句块后面还可以跟上 <font style="color:rgb(71, 101, 130);">finally</font> 语句块,不管有没有发生异常,<font style="color:rgb(71, 101, 130);">finally</font> 语句块中的代码都会被执行。

- 没有发生异常时
1
try:print(1 / 1)except ZeroDivisionError:print("ZeroDivisionError happened!")else:print("Exception not happened")finally:print("Finally is executed!")print("Done!")
2
3
4
5
6
7
8
9
10 没有发生异常时,除了执行
<font style="color:rgb(71, 101, 130);">else</font> 语句块中的代码,输出了 <font style="color:rgb(71, 101, 130);">Exception not happened</font>,<font style="color:rgb(71, 101, 130);">finally</font> 语句块中的代码也得到了执行,输出了 <font style="color:rgb(71, 101, 130);">Finally is executed!</font> 。
- 发生异常时
1
try:print(1 / 0)except ZeroDivisionError:print("ZeroDivisionError happened!")else:print("Exception not happened")finally:print("Finally is executed!")print("Done!")
2
3
4
5
6
7
8
9
10 发生异常时,
<font style="color:rgb(71, 101, 130);">finally</font> 语句块中的代码同样得到了执行,输出了 <font style="color:rgb(71, 101, 130);">Finally is executed!</font> 。因此,不管有没有发生异常,<font style="color:rgb(71, 101, 130);">finally</font> 语句块中的代码都会被执行。<font style="color:rgb(71, 101, 130);">finally</font> 在释放资源时会特别有用。
#3 抛出异常
前面讲的是异常出现时,可以做的处理。另外,我们也可以主动抛出异常。主动抛出异常使用<font style="color:rgb(71, 101, 130);">raise</font> 关键字。举个例子:
1
x = 10if x > 5:raise Exception('x should not exceed 5. The value of x was: {}'.format(x))
2
3 当
<font style="color:rgb(71, 101, 130);">x > 5</font> 时,主动抛出异常。
#4 总结

#5 练习题
打开一个不存在的文件,对发生的异常进行处理。更新于: 12/30/2021, 2:46:39 AM
—— 转眼间,我们的课程进入到了尾声阶段,不知道大家在课程的学习过程中有没有感受到 Python 的魅力呢! 最后一关,我们来一场华丽的华山论剑,我们将前面十三关的重要知识点浓缩成下面两个问题,大家一起来比拼下 Python 的武力值,为我们的课程画上一个完美的句号。- 实现一个猜数字游戏,首先使用
<font style="color:rgb(71, 101, 130);">random</font>模块中的随机函数生成一个位于<font style="color:rgb(71, 101, 130);">[1, 100]</font>范围内的数字,然后让机器人玩家来猜,比较机器人玩家猜的数字和生成的随机数,如果随机数大于玩家猜的数字告诉玩家猜的数字过小,如果随机数小于玩家猜的数字告诉玩家猜的数字过大,直到玩家猜到正确的数字。
<font style="color:rgb(71, 101, 130);">1~100</font> 的随机数可以使用 <font style="color:rgb(71, 101, 130);">random.randint(1, 100)</font>
1. 实现一个剪刀、石头、布的游戏,首先使用 <font style="color:rgb(71, 101, 130);">random</font> 模块的函数从列表 <font style="color:rgb(71, 101, 130);">['剪刀', '石头', '布']</font> 中随机选择一个,然后机器人玩家也随机出一个,比较两个,判断玩家是输是赢。最后给出机器人玩家赢了几次,输了几次,平了几次。
提示:从列表 <font style="color:rgb(71, 101, 130);">'剪刀', '石头', '布']</font> 随机选择,可以使用 <font style="color:rgb(71, 101, 130);">random.choice(['剪刀', '石头', '布']</font>
