尝试捕获异常
回忆上次内容
先试试看
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_bc](https://file.cfanz.cn/uploads/png/2023/04/24/12/8Cd4Y41E02.png)
简化问题
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_bc_02](https://file.cfanz.cn/uploads/png/2023/04/24/12/d5PT029aK5.png)
思路
- 出现在倒数第二行
- total = int(a) + int(b)
#!/usr/bin/python3
a = input("How many apples do you have: ")
print("You have got " + a + " apples!")
b = input("How many banana do you have: ")
print("You have got " + b + " bananas!")
total = int(a) + int(b)
print("You have got " + str(total) + " fruits in all!")
搜索 try
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_03](https://file.cfanz.cn/uploads/png/2023/04/24/12/39Gc49MZ4F.png)
try
- 就继续运行 try里面的代码
- 直到完成 try中所有的代码
试着来
#!/usr/bin/python3
a = input("How many apples do you got?\\n")
print("You got " + a + " apples!")
b = input("How many bananas do you got?\\n")
print("You got " + b + " bananas!")
try:
total = int(a) + int(b)
except:
print("\\33[41m[error]\\33[0m -- input should be numbers!")
print("You got " + str(total) + " fruits!")
- 待尝试的内容需要缩进 4 个字符
- total = int(a) + int(b)
- 所有需要try的内容都要
尝试运行
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_04](https://file.cfanz.cn/uploads/png/2023/04/24/12/Pc34TIcXCc.png)
- 由于int("a") 会出现错误
- 根本没有被声明
- 而且 后面还要被引用
再次修正
#!/usr/bin/python
a = input("How many apples do you got?\\n")
print("You got " + a + " apples!")
b = input("How many bananas do you got?\\n")
print("You got " + b + " bananas!")
try:
total = int(a) + int(b)
print("You got " + str(total) + " fruits!")
except:
print("\\33[41m[error]\\33[0m -- input should be numbers!")
尝试
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_05](https://file.cfanz.cn/uploads/png/2023/04/24/12/1DWZ8B5W99.png)
- 或者说我接管了相关的报错信息
- 对于这类错有了自己的处理
- 这就是捕获了异常
- 并且处理了异常
不报错
#!/usr/bin/python
a = input("How many apples do you got?\\n")
print("You got " + a + " apples!")
b = input("How many bananas do you got?\\n")
print("You got " + b + " bananas!")
try:
total = int(a) + int(b)
print("You got " + str(total) + " fruits!")
except:
pass
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_06](https://file.cfanz.cn/uploads/png/2023/04/24/12/8Z4S1W5f8H.png)
对应禅语
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_07](https://file.cfanz.cn/uploads/jpeg/2023/04/24/12/83GP012d92.jpeg)
- 不写 except:pass 风格的代码
- 那什么是 except:pass 风格的代码呢?
异常飘过
- 虽然 except接收到了错误
- 但是 轻轻飘过pass
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_08](https://file.cfanz.cn/uploads/png/2023/04/24/12/1X9CBEb7W5.png)
- 这是最要命的
- 让人没有追踪trace的痕迹
- 无法调试debug!
- 无语问苍天😭!
保留报错
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_缩进_09](https://file.cfanz.cn/uploads/png/2023/04/24/12/S737734177.png)
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_bc_10](https://file.cfanz.cn/uploads/png/2023/04/24/12/5H13aaO9eP.png)
保留报错细节
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_python_11](https://file.cfanz.cn/uploads/png/2023/04/24/12/S1L7HFb79Q.png)
![图片描述 [oeasy]python0139_尝试捕获异常_ try_except_traceback_bc_12](https://file.cfanz.cn/uploads/png/2023/04/24/12/a5X98Ybe42.png)
总结
- 不要忽略、隐瞒
- 否则找不到出错位置
- 还可以用traceback把
- 我们下次再说!👋
- 蓝桥->https://www.lanqiao.cn/courses/3584
- github->https://github.com/overmind1980/oeasy-python-tutorial
- gitee->https://gitee.com/overmind1980/oeasypython