python-异常(笔记)

python 复制代码
#后续代码可以正常运行
try:
   f= open("xxx.txt","r",encoding='utf-8')

except:
    print("except error")
python 复制代码
#捕获指定异常,其他异常报错程序中止,管不到
try:
   print(name)
except NameError as you_call:
    print("name error")
python 复制代码
#打印异常
try:
   print(name)
except NameError as you_call:
    print(f"name error {you_call}")

name error name 'name' is not defined
python 复制代码
try:

   f=1/0
except (NameError,ZeroDivisionError) as you_call:
    print(f"name or math error {you_call}")

name or math error division by zero



try:

   print(name)
except (NameError,ZeroDivisionError) as you_call:
    print(f"name or math error {you_call}")

name or math error name 'name' is not defined
python 复制代码
#捕获所有的异常(某行代码出错就会被捕获到)
import math

try:
    
    math.e()
except Exception as e:
    print(f"error {e}")

error 'float' object is not callable

异常的传递性:

相关推荐
武子康1 天前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
大大大大晴天1 天前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋2 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python