目标
- 了解 python 常见的异常处理
- 掌握捕获异常的语句
1.常见异常
异常 | 说明 |
---|
SyntaxError | 语法错误 |
AttributeError | 属性异常 |
TypeError | 类型异常 |
ZeroDivisionError | 除数为0 |
2.处理异常
2.1 try…except…
- 把某个程序语句写进 try…except 中,如果没有异常,程序会忽略except子句,正常运行;如果 try 子句中发生异常,程序会直接跳到 except 部分,执行子句。
try:
...
except:
...
- 以上模式会捕获任何一种异常,没有针对性,难以检测是何种异常
- 以下实例
while 1:
print('这是一个除法计算游戏')
c=input("输入‘c’游戏继续,否则游戏终止")
if c=='c':
a=10
b=input("输入除数")
try:
print("除法结果是",a/float(b))
print('--'*20)
except:
print("不知道啥异常,反正就是异常")
print('--'*20)
else:
break
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224111753639.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xhbGFsYWxhenk=,size_16,color_FFFFFF,t_70)
try:
...
except ZeroDivisionError:
...
try:
...
except ZeroDivisionError as e:
print(e)
- 以上是捕获除数为0的异常,如果想要捕获别的异常,把异常类型代入即可。
- 以下实例
while 1:
print('这是一个除法计算游戏')
c=input("输入‘c’游戏继续,否则游戏终止")
if c=='c':
a=input("输入第一个数")
b=input("输入第二个数")
try:
print("除法结果是",float(a)/float(b))
print('--'*20)
except ZeroDivisionError:
print("第二个数不能为0")
print('--'*20)
else:
break
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224105352203.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xhbGFsYWxhenk=,size_16,color_FFFFFF,t_70)
- python 在处理程序时遇到异常,程序就会终止运行,当你把你觉得可能会出错的语句放在 try 语句之中,让python帮你检查错误、处理异常,程序会继续运行。
2.2 处理多个异常
- try…except…还可以处理多个异常
- 方法一:使用多个except
while 1:
print('这是一个除法计算游戏')
c=input("输入‘c’游戏继续,否则游戏终止")
if c=='c':
a=input("输入第一个数")
b=input("输入第二个数")
try:
print("除法结果是",float(a)/float(b))
print('--'*20)
except ZeroDivisionError:
print("第二个数不能为0")
print('--'*20)
except ValueError:
print("请输入数字")
print('--'*20)
else:
break
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224110347340.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xhbGFsYWxhenk=,size_16,color_FFFFFF,t_70)
while 1:
print('这是一个除法计算游戏')
c=input("输入‘c’游戏继续,否则游戏终止")
if c=='c':
a=input("输入第一个数")
b=input("输入第二个数")
try:
print("除法结果是",float(a)/float(b))
print('--'*20)
except (ZeroDivisionError,ValueError) as e:
print(e)
print('--'*20)
else:
break
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224112815359.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xhbGFsYWxhenk=,size_16,color_FFFFFF,t_70)
5.抛出异常
5.1 raise
- 发起异常
raise NameError('python')
raise NameError('python')
try:
python
except NameError as e:
print(e)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224114810660.png)
- 以上把 python 定义为一个叫 NameError 的异常,
5.2 assert
x=-2
assert x<0
assert x>0
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224115100364.png)
5.3 抛出异常的实际应用
while 1:
try:
age=int(input('输入你的年龄'))
assert age>0
if age > 150:
raise ValueError('输入年龄超出了正常范围')
elif (age<=150) and (age>80):
print("您真长寿,祝福如东海")
elif (age<=80) and (age>=60):
print("该享福啦")
elif (age<=60) and (age>=30):
print("壮年")
else:
print("真年轻")
except (ValueError,AssertionError) as e:
print(e)
break
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224120450558.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xhbGFsYWxhenk=,size_16,color_FFFFFF,t_70)