对Python中内置异常层次结构详解


Posted in Python onOctober 18, 2018

如下所示:

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
  +-- StopIteration
  +-- StandardError
  | +-- BufferError
  | +-- ArithmeticError
  | | +-- FloatingPointError
  | | +-- OverflowError
  | | +-- ZeroDivisionError
  | +-- AssertionError
  | +-- AttributeError
  | +-- EnvironmentError
  | | +-- IOError
  | | +-- OSError
  | |   +-- WindowsError (Windows)
  | |   +-- VMSError (VMS)
  | +-- EOFError
  | +-- ImportError
  | +-- LookupError
  | | +-- IndexError
  | | +-- KeyError
  | +-- MemoryError
  | +-- NameError
  | | +-- UnboundLocalError
  | +-- ReferenceError
  | +-- RuntimeError
  | | +-- NotImplementedError
  | +-- SyntaxError
  | | +-- IndentationError
  | |   +-- TabError
  | +-- SystemError
  | +-- TypeError
  | +-- ValueError
  |   +-- UnicodeError
  |    +-- UnicodeDecodeError
  |    +-- UnicodeEncodeError
  |    +-- UnicodeTranslateError
  +-- Warning
   +-- DeprecationWarning
   +-- PendingDeprecationWarning
   +-- RuntimeWarning
   +-- SyntaxWarning
   +-- UserWarning
   +-- FutureWarning
  +-- ImportWarning
  +-- UnicodeWarning
  +-- BytesWarning

想要捕获所有的异常,可以直接捕获 Exception 即可:

try:
 ...
except Exception as e:
 ...
 log('Reason:', e)  # Important!

这个将会捕获除了 SystemExit 、 KeyboardInterrupt 和 GeneratorExit 之外的所有异常。 如果你还想捕获这三个异常,将 Exception 改成 BaseException 即可。

以上这篇对Python中内置异常层次结构详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中用format函数格式化字符串的用法
Apr 08 Python
在Python中使用next()方法操作文件的教程
May 24 Python
在Python的Django框架中编写编译函数
Jul 20 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
Nov 07 Python
CentOS7.3编译安装Python3.6.2的方法
Jan 22 Python
对python中Json与object转化的方法详解
Dec 31 Python
python3转换code128条形码的方法
Apr 17 Python
使用Python3内置文档高效学习以及官方中文文档
May 19 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
Sep 10 Python
基于pytorch的lstm参数使用详解
Jan 14 Python
如何基于pythonnet调用halcon脚本
Jan 20 Python
Python jiaba库的使用详解
Nov 23 Python
Python运维开发之psutil库的使用详解
Oct 18 #Python
python实现自动登录后台管理系统
Oct 18 #Python
python 对给定可迭代集合统计出现频率,并排序的方法
Oct 18 #Python
python实现简单登陆系统
Oct 18 #Python
Python字典中的键映射多个值的方法(列表或者集合)
Oct 17 #Python
python字典值排序并取出前n个key值的方法
Oct 17 #Python
Python中创建二维数组
Oct 17 #Python
You might like
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
2012/06/13 PHP
PHP随机获取未被微信屏蔽的域名(微信域名检测)
2017/03/19 PHP
javascript对下拉列表框(select)的操作实例讲解
2013/11/29 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
node.js中的fs.fchownSync方法使用说明
2014/12/16 Javascript
JS实现向表格行添加新单元格的方法
2015/03/30 Javascript
JavaScript获取并更改input标签name属性的方法
2015/07/02 Javascript
原生js实现节日时间倒计时功能
2017/01/18 Javascript
js输入框使用正则表达式校验输入内容的实例
2017/02/12 Javascript
Node.js与Sails redis组件的使用教程
2017/02/14 Javascript
Vue form 表单提交+ajax异步请求+分页效果
2017/04/22 Javascript
Node.JS更改Windows注册表Regedit的方法小结
2017/08/18 Javascript
浅谈jquery fullpage 插件增加头部和版权的方法
2018/03/20 jQuery
Python程序语言快速上手教程
2012/07/18 Python
利用Python的Django框架生成PDF文件的教程
2015/07/22 Python
python 中split 和 strip的实例详解
2017/07/12 Python
简单了解python数组的基本操作
2019/11/26 Python
tensorflow 获取所有variable或tensor的name示例
2020/01/04 Python
keras 解决加载lstm+crf模型出错的问题
2020/06/10 Python
解决pytorch 交叉熵损失输出为负数的问题
2020/07/07 Python
Python生成pdf目录书签的实例方法
2020/10/29 Python
HTML5本地存储之Database Storage应用介绍
2013/01/06 HTML / CSS
Amcal中文官网:澳洲综合性连锁药房
2019/03/28 全球购物
澳大利亚家庭花园和DIY工具网店:VidaXL
2019/05/03 全球购物
常用UNIX 命令(Linux的常用命令)
2013/07/10 面试题
项目建议书范文
2014/05/12 职场文书
三方股东合作协议书范本
2014/09/28 职场文书
合作协议书模板
2014/10/10 职场文书
一年级小学生评语大全
2014/12/25 职场文书
父亲节寄语大全
2015/02/27 职场文书
运动会100米加油稿
2015/07/21 职场文书
2016年党员岗位承诺书
2016/03/24 职场文书
导游词之宁夏贺兰山岩画
2019/11/08 职场文书
vue打包时去掉所有的console.log
2022/04/10 Vue.js
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS
Python使用plt.boxplot()函数绘制箱图、常用方法以及含义详解
2022/08/14 Python