惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
Scott Helme
Scott Helme
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
C
Cyber Attacks, Cyber Crime and Cyber Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
I
Intezer
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
V2EX - 技术
V2EX - 技术
Google Online Security Blog
Google Online Security Blog
L
Lohrmann on Cybersecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 热门话题
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Latest news
Latest news
B
Blog
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
L
LangChain Blog
GbyAI
GbyAI
Last Week in AI
Last Week in AI
S
Security Affairs
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
Security Latest
Security Latest
Vercel News
Vercel News
Y
Y Combinator Blog
G
GRAHAM CLULEY
S
Securelist
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
雷峰网
雷峰网

博客园 - emanlee

mysql 在新电脑上导入,出现错误:ERROR at line 4019: Unknown command '\''. - emanlee 安装 yarn npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。 - emanlee VS code, 最近打开过哪些项目或文件夹,把这些备份下来,到另外一台电脑上恢复 傲梅分区助手 【好用】 ubuntu 可以使用Windows远程桌面连接吗?【可以】 把xshell的全部连接和密码,从一台Windows 10电脑,迁移到Windows11电脑。 Windows 远程桌面(mstsc)完整迁移方案(含历史连接记录、RDP 配置、保存的账号密码)【可行】 Endnote,如何能完整迁移库和模版 ubuntu 安装 locate “华为杯”第八届中国研究生人工智能创新大赛 当数据量很大时,执行 mysql> source filename.sql 很慢,有什么办法更快 pip install django-ranged-response==0.2.0 部署Vue+Django 04c,mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory Ubuntu Server 24.04 安装 MySQL, ./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory ubuntu server 24.04: 如何设置默认采用 Xorg 方式登录 Ubuntu Server 24.04 设置锁屏时间 Ubuntu Server 启动过程中,比较慢 Job systemd-networkd-wait-online.service/start running Ubuntu Server 24.04 启用root用户 Ubuntu snap install 安装todesk是否必须检查 CPU 是否支持 AVX / AVX2 命令 Ubuntu Server 24.04下解决SunloginClient 向日葵依赖libgconf-2-4安装问题 Ubuntu Server 24.04 安装图形界面 ubuntu server 24.04 安装 中文输入法 Ubuntu Server 24.04 安装浏览器firefox Ubuntu Server 24.04 安装todesk【先联网,CPU必须支持 AVX / AVX2 】 Ubuntu Server 24.04 安装中文,汉字 Python赋值,什么时候传引用? 大驼峰命名法 python 容易理解类和对象的例子 django+mysql: 如何添加一个新的超级用户? Python 匿名函数(lambda)举例 Python函数内部的局部变量和全局变量 订书钉规格 在Navicat中复制带有自动增长id字段的表格,如何把新表格的自动增长id重新设置为从1开始 科研02 期刊和会议的质量等级 Vue 2,父组件中点击一个按钮,更新子组件(重新加载子组件) 汉明距离(Hamming distance) Modbus slave模拟的从站 如何主动发送数据给主站 Word中,表格中的文字在一行的偏下方,没有垂直居中 在线把汉字转拼音字母 Navicat中复制并修改MySQL表,保存时,出现未知的列的错误 WinSCP 便携版、绿色版 下载 centos / Linux 上,非管理员用户安装 miniconda FTP 服务器设置(FileZilla Server) Windows主机上的Device/Credential Guard功能与VMware Workstation虚拟化冲突导致 Navicat 备份连接
Python 异常体系核心:BaseException 及其四大子类
emanlee · 2026-04-04 · via 博客园 - emanlee

Python 异常体系核心:BaseException 及其四大子类

这是 Python 异常继承体系的顶层结构,是理解异常捕获、处理逻辑的核心基础


一、总览:Python 异常金字塔顶端 

BaseException(所有异常的祖宗)
├─ Exception(业务/程序异常的基类)
├─ KeyboardInterrupt(用户强制中断)
├─ GeneratorExit(生成器退出)
└─ SystemExit(解释器主动退出)

一句话总结

BaseException 是所有异常的最终父类,它直接派生了 4 个子类,这 4 个类分工完全不同,决定了 Python 异常的捕获规则。


二、逐类详细讲解(最关键)

1. BaseException

  • 定义:Python 异常体系的根类、基类、超类
  • 地位所有异常都继承自它
  • 作用:定义异常的基础结构(message、args、__str__等)
  • 注意不建议直接捕获 BaseException,会把系统退出、用户中断全部拦截,导致程序无法正常退出。

2. Exception(最重要)

  • 地位所有 “业务 / 程序运行异常” 的父类
  • 范围
    • 语法错误之外的所有程序错误
    • 内置异常:ValueErrorTypeErrorIndexErrorKeyErrorAttributeErrorIOErrorZeroDivisionError
    • 自定义异常(必须继承它)
  • 作用:代表程序运行中出现的可处理异常
  • 规范: ✅ 日常开发 只应该捕获 Exception 或其子类 ❌ 不要捕获 BaseException

3. KeyboardInterrupt

  • 触发条件:用户在控制台按 Ctrl + C 强制终止程序
  • 继承:直接继承 BaseException,不属于 Exception
  • 特点
    • 专门用于终止程序
    • 如果你写 except Exception: 捕获不到它
  • 用途:让用户可以随时中断死循环 / 长时间任务

4. GeneratorExit

  • 触发条件生成器(generator)被关闭时
    • 调用 gen.close()
    • 生成器被垃圾回收
  • 继承:直接继承 BaseException
  • 特点:不属于 Exception,普通捕获抓不到
  • 用途:用于生成器内部资源清理

5. SystemExit

  • 触发条件:Python 解释器主动退出
    • sys.exit()
    • exit()
    • quit()
  • 继承:直接继承 BaseException
  • 特点不是错误,是正常退出信号
  • 作用:控制程序优雅退出,不会被 except Exception 拦截

三、最关键的规则(面试 / 开发必考)

✔ 规则 1

Exception 只能捕获程序异常,不能捕获系统退出类异常

下面这些都不会except Exception: 捕获:

  • KeyboardInterrupt(Ctrl+C)
  • SystemExit(sys.exit)
  • GeneratorExit(生成器关闭)

它们都直接继承 BaseException。

✔ 规则 2

只有 except BaseException: 才能捕获所有异常,但严禁乱用

✔ 规则 3

自定义异常必须继承 Exception,不能继承 BaseException


四、一张表彻底记住

异常类名父类含义能否被 except Exception 捕获
BaseException 无(根) 所有异常的祖宗
Exception BaseException 程序异常基类 ✅ 能
KeyboardInterrupt BaseException Ctrl+C 中断 ❌ 不能
GeneratorExit BaseException 生成器关闭 ❌ 不能
SystemExit BaseException 解释器退出 ❌ 不能

五、最标准的捕获写法(开发规范)

try:
    业务代码
except Exception as e:
    只处理程序错误
    # 不会拦截 Ctrl+C、sys.exit、生成器关闭

六、一句话终极总结

BaseException 生了四个孩子:

Exception 管程序错误,

KeyboardInterrupt 管用户中断,

SystemExit 管解释器退出,

GeneratorExit 管生成器关闭。

只有 Exception 是程序可处理异常,另外三个都属于系统控制流。