python学习笔记--异常捕获

异常场景

复制代码
num=input("input you number:")
n=9000
try:
    result=n/int(num)
    print('{} 除以num 结果为{}'.format(n,result))
except ZeroDivisionError as err:
    print("0不可以作为除数,出现报错{}".format(err))
except ValueError as err:
    print("请检查输入的数据类型{}是否为数字".format(err))

多重异常

最后一个exception作为保底

多重异常,可以合并到一起

异常嵌套

有因果关系的才需要嵌套,前面的结果异常了,对于后边的程序没有意义

相关推荐
曲幽2 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户8356290780517 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon8 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly8 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程8 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly10 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风17 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风17 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风2 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python