如何处理各种可能的异常_try_except_Error
回忆上次内容
- 我们了解了 try 的细节
- 注意!
- 都要有英文半角的冒号
- 子句都要通过 4 个字符的缩进控制范围
文档提示
各种错误
try:
a
i = int("123")
j = i / 0
except ValueError:
print(ValueError)
except ZeroDivisionError:
print(ZeroDivisionError)
except:
print("Something is Wrong!")
else:
print("Nothing Wrong")
一网不捞鱼
- 一网不捞鱼
- 二网不捞鱼
- 三网捞个小尾巴尾巴尾巴尾巴尾巴尾巴……鱼
- 第三网
- 是通用模式except
- 可以捕获到 所有的异常
- 一网打尽
- 如果这句except排在前面的话
尝试调换位置
运行结果
- 要 求通用异常处理方式
- except的 位置
- 必须 是 最后一个
- 通用的处理 得保底
明确含义
try:
a
i = int("123")
j = i / 0
except ValueError:
print(ValueError)
except ZeroDivisionError:
print(ZeroDivisionError)
except Exception:
print(Exception)
else:
print("Nothing Wrong")
输出结果
修改代码
try:
a
i = int("123")
j = i / 0
except ValueError:
print(ValueError)
except ZeroDivisionError:
print(ZeroDivisionError)
except Exception as e:
print(e)
else:
print("Nothing Wrong")
尝试调试
找到通用处理方式
捕获异常
try:
a
i = int("123")
j = i / 0
except NameError:
print(NameError)
except ValueError:
print(ValueError)
except ZeroDivisionError:
print(ZeroDivisionError)
except Exception as e:
print(e)
else:
print("Nothing Wrong")
- 在运行过程中
- 出现了NameError
- 在 第一个 尝试捕获异常的位置
- 进入except NameError子句
- 跳过后面所有 except
- 跳过else
del
except
capture
captive
accept
concept
- concept
- con 一起
- cept 拿
- 一起拿
- 把想法放在一起拿起来
- 整合想法
conceive
perceive
deceive
- deceive
- de 向下
- cept 拿
- 向下拿
- 借助信息的不对等 去拿
receive
cept 总结
总结
- 我们下次再说!
- 蓝桥->https://www.lanqiao.cn/courses/3584
- github->https://github.com/overmind1980/oeasy-python-tutorial
- gitee->https://gitee.com/overmind1980/oeasypython