理解Python语言里的异常(Exception)
Exception is as a sort of structured "super go to".
异常是一种结构化的"超级goto".
作为一个数十年如一日地钟爱C语言的程序员(因为C程序员需要记忆的关键字很少,而且可以很惬意地玩内存),对于高级语言如Python里的异常(Exception)一直不甚理解,尤其是其实现机理。但读了《Learning Python》一书中上面这句话(尤其是goto关键字)后,忽然豁然开朗。
如果用C语言编写一个鲁棒性良好的程序,一般得在每一个可能出错的运算之后检查返回值或状态码,然后在程序执行的时候根据返回值或状态做出不同的处理。例如:
int doStuff()
{ /* C program */
if (doFirstThing() == ERROR) /* Detect errors everywhere */
return ERROR; /* even if not handled here */
if (doNextThing() == ERROR)
return ERROR;
...
return doLastThing();
} int main()
{
if (doStuff() == ERROR)
badEnding();
else
goodEnding();
...
}
实际上,在现实的C程序中,通常用于处理错误检查和用于实际工作的代码数量相当。 但是, 在Python中,程序员就不用那么小心翼翼和神经质了。你可以把程序的任意片段包装在异常处理器内,然后编写从事实际工作的部分,假设一切都工作正常。 例如:
def doStuff(): # Python code
doFirstThing() # We don't care about exceptions here,
doNextThing() # so we don't need to detect them
...
doLastThing() if __name__ == '__main__':
try:
doStuff() # This is where we care about results,
except: # so it's the only place we must check
badEnding()
else:
goodEnding()
...
在Python代码中,完全没有必要让所有代码都去预防错误的发生,因为一旦有异常发生,运行Python代码的控制权就会立即跳转到相应的异常处理程序。再者,因为Python解释器会自动检查错误,所以Python代码通常不需要事先检查错误。归根结底一句话,异常(Exception)让程序员大致上可以忽略罕见的情况,并避免编写那些(烦人的但又不得不写的)错误检查代码。 //英文原文如下:
Because control jumps immediately to a handler when an exception occurs, there's
no need to instrument all your code to guard for errors. Moreover, because
Python detects errors automatically, your code usually doesn’t need to check for
errors in the first place. The upshot is that exceptions let you largely ignore
the unusual cases and avoid error-checking code.
1. Why Use Exceptions? 为什么使用异常
In a nutshell, exceptions let us jump out of arbitrarily large chunks of a program.
简而言之,异常让我们从一个程序中任意大的代码块中跳将出来。

2. Exception Roles 异常充当的最常见的几种角色
- Error handling 错误处理
- Event notification 事件通知
- Special-case handling 特殊情况处理
- Termination actions 行为终止
- Unusual control flows 非常规控制流


3. Exceptions: The Short Story 异常处理简明教程
3.1 默认异常处理器 (Default Exception Handler)
当我们的代码没有刻意去捕获某个异常的时候,一旦有致命错误发生,解释器将启动默认的异常处理器,例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def fetcher(obj, index):
... return obj[index]
...
>>> x = 'spam'
>>> fetcher(x, 3)
'm'
>>>
>>> fetcher(x, 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fetcher
IndexError: string index out of range
>>>
3.2 捕获异常 (Catching Exceptions)
很多时候,我们并不希望执行默认的异常行为,而是即便异常发生之后,我们的代码还能继续运行下去。这样,我们可以自己捕获异常。例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>> def fetcher(obj, index):
... return obj[index]
...
>>> def catcher(obj, index):
... try:
... fetcher(obj, index)
... except IndexError:
... print "got exception"
... print "continuing"
...
>>>
>>> x = 'spam'
>>>
>>> catcher(x, 3)
continuing
>>>
>>> catcher(x, 4)
got exception
continuing
>>>
这里,我们使用了try ... except ...捕获异常。
3.3 引发异常 (Raising Exceptions)
异常能有Python解释器引发,当然也能由我们自己写的Python程序引发。
3.3.1 无条件引发异常 (raise)
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>> raise IndexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError
>>>
>>>
>>> try:
... raise IndexError
... except IndexError:
... print "got exception"
...
got exception
>>>
如果没捕捉异常,用户定义的异常就会向上传递,直到顶层默认的异常处理器,并通过标准出错信息终止该程序。
3.3.2 有条件引发异常 (assert)
assert也可以用来引发异常,它是一个有条件的raise,主要在开发过程中用于调试。例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>>
>>> assert False, "Nobody expects the Spanish Inquisition!"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Nobody expects the Spanish Inquisition!
>>>
>>> assert True, "Nobody expects the Spanish Inquisition!"
>>>
3.4 用户定义的异常 (User-Defined Exceptions)
在3.3.1中,使用raise语句引发的异常是Python的内置作用域中定义的一个内置异常。当然,我们也可以定义自己的异常。用户定义的异常能够通过类编写,它继承一个内置的异常类:通常这个类的名称叫做Exception。基于类的异常允许脚本建立异常类型、继承行为以及附加状态信息。例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>>
>>> class Bad(Exception): # user-defined exception
... pass
...
>>> def doomed():
... raise Bad() # raise an instance
...
>>> try:
... doomed()
... except Bad: # catch class name
... print "got Bad"
...
got Bad
>>>
3.5 终止行为 (Termination Actions)
Finally, try statements can say "finally" -- that is, they may include finally blocks. These look like except handlers for exceptions, but the try/finally combination specifies termination actions that always execute "on the way out," regardless of whether an exception occurs in the try block.
最后,try语句可以说"finally"。也就是说,它可以包含finally代码块。这看上去就像异常的except处理器,但是try/finally组合,可以定义一定会在最后执行时的收尾行为,无论try代码块是否发生了异常。 例如:
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>>
>>> def fetcher(obj, index):
... return obj[index]
...
>>> x = 'spam'
>>> try:
... fetcher(x, 3)
... finally: # Termination actions
... print "after fetch"
...
'm'
after fetch
>>>
>>> try:
... fetcher(x, 4)
... finally: # Termination actions
... print "after fetch"
...
after fetch
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in fetcher
IndexError: string index out of range
>>>
由此可见,无论有没有异常发生,都会执行finally子句。 当然,在实际应用中,我们通常使用try ... expect .... finally组合。 try/except组合用于捕获异常并从中恢复,而try/finally组合确保无论try代码块内的代码是否发生了异常,终止行为一定会运行。(典型的应用是,没有异常按照正常流程走,有异常的时候则执行Error-handing操作;任何情况下最后都做cleanup操作)。例如:
veli$ ls -l /tmp/foo.txt
-rw-r--r-- 1 root root 12 Jun 4 18:19 /tmp/foo.txt
veli$ ls -l /tmp/bar.txt
-rw-r--r-- 1 veli veli 0 Jun 4 21:33 /tmp/bar.txt veli$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>> def writer(file, s):
... fd = None
... try:
... fd = open(file, "w")
... fd.write("%s\n" % s)
... except:
... print "ERROR: fail to open file %s" % file
... finally:
... print "close file"
... if fd is not None:
... fd.close()
...
>>> s="hello world again"
>>> writer("/tmp/foo.txt", s)
ERROR: fail to open file /tmp/foo.txt
close file
>>>
>>> writer("/tmp/bar.txt", s)
close file
>>> veli$ ls -l /tmp/foo.txt
-rw-r--r-- 1 root root 12 Jun 4 18:19 /tmp/foo.txt veli$ ls -l /tmp/bar.txt && cat /tmp/bar.txt
-rw-r--r-- 1 veli veli 18 Jun 4 21:34 /tmp/bar.txt
hello world again
上面的writer()使用的是try ... except ... finally,可以用with ... as ...代替,例如:
veli$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
...<snip>...
>>> def writer(file, s):
... with open(file, "w") as fd:
... fd.write("%s\n" % s)
...
>>>
>>> s = "hello world again"
>>>
>>> writer("/tmp/foo.txt", s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in writer
IOError: [Errno 13] Permission denied: '/tmp/foo.txt'
>>>
>>> writer("/tmp/bar.txt", s)
>>>
3.6 小结 (Summary)
| Statement | Meaning |
| try/except | Catch and recover from exceptions raised by Python, or by you. |
| try/finally | Perform cleanup actions, whether exceptions occur or not. |
| raise | Trigger an exception manually in your code. |
| assert | Conditionally trigger an exception in your code. |
| with/as | Implement context managers in Python 2.6+. |
参考资料:
1. Book: Learning Python, Fourth Edition : PART VII Exceptions and Tools
理解Python语言里的异常(Exception)的更多相关文章
- Java 里的异常(Exception)详解
作为一位初学者, 本屌也没有能力对异常谈得很深入. 只不过Java里关于Exception的东西实在是很多. 所以这篇文章很长就是了.. 一, 什么是java里的异常 由于java是c\c++ ...
- 010 深入理解Python语言
目录 一.概述 二.计算机技术的演进 2.1 计算机技术的演进过程 三.编程语言的多样初心 3.1 编程语言有哪些? 3.2 不同编程语言的初心和适用对象 3.3 2018年以后的计算环境- 四.Py ...
- 深入理解python语言
2008年,安卓操作系统诞生:PC时代向移动时代转换 互联网,视窗 2017/5/27柯洁最终0:3AlphaGo 计算机技术的演进过程 不同编程语言的设计初心和适用对象 C语言核心解决的是性能问题, ...
- 第三章 深入理解python语言
计算机技术的演进过程 1946-1981年 计算机系统结构时代(35年) 解决计算机能力的问题 1981-2008年 网络和视窗时代(27年) 解决交互问题 2008-2016年 复杂信息系统时代(8 ...
- 理解 Python 语言中的 defaultdict
众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键都存在默认值是非常 ...
- Python logging 模块打印异常 exception
logger.exception(sys.exc_info())
- Python语言学习之Python入门到进阶
人们常说Python语言简单,编写简单程序时好像也确实如此.但实际上Python绝不简单,它也是一种很复杂的语言,其功能特征非常丰富,能支持多种编程风格,在几乎所有方面都能深度定制.要想用好Pytho ...
- 【学习笔记】PYTHON语言程序设计(北理工 嵩天)
1 Python基本语法元素 1.1 程序设计基本方法 计算机发展历史上最重要的预测法则 摩尔定律:单位面积集成电路上可容纳晶体管数量约2年翻倍 cpu/gpu.内存.硬盘.电子产品价格等都遵 ...
- 《Python语言程序设计》【第1周】Python基本语法元素
实例:温度转化 #TempConvert.py 单行注释 ''' TemConvert.py ''' # 多行注释 TempStr = input("请输入带有符号的温度值: ") ...
随机推荐
- CentOS 新系统后配置
1. 网络配置 略 1.2 ip_froward 查看 sysctl -a | grep ip_ 修改 vi /etc/sysctl.conf net.ipv4.ip_forward = 1 最大使用 ...
- BFC开启条件
当元素CSS属性设置了下列之一时,即可创建一个BFC: float:left|right position:absolute|fixed display: table-cell|table-capti ...
- Oracle索引技术研究
Oracle索引类型 B树索引 特定类型索引 确定索引列 主键和唯一键值列的索引 外键索引 其他合适的索引列 B树索引 B树索引算法 B树是指B-tree(Balanced Tree),B树的存在是为 ...
- C#不能捕捉的异常,如AccessViolationException
在.net的异常机制中,有部分严重的编程错误(系统的某些Corrupted State Exceptions异常)是默认不被用户使用常规的异常捕捉方式捕捉到的. 微软的这种设计方式,是让用户必须处理该 ...
- 会HTML/CSS就可以轻松创建网站
网站其本质就是HTML + CSS 外加一些JavaScript构成的.所以基本上只要你会一些前端,就可以开始花样搭网站了. 如果只用HTML/CSS那做出来的网站只能叫静态网站,性能好但维护不方便, ...
- 虚拟化 - VMware
和VirtualBox一样,也需要关掉Hyper-V才能启动虚拟机,否则会报Guard的错误. 网络 [转]VMware网络连接模式-桥接.NAT以及仅主机模式的详细介绍和区别 桥接 就好像在局域网中 ...
- python网络编程--线程(锁,GIL锁,守护线程)
1.线程 1.进程与线程 进程有很多优点,它提供了多道编程,让我们感觉我们每个人都拥有自己的CPU和其他资源,可以提高计算机的利用率.很多人就不理解了,既然进程这么优秀,为什么还要线程呢?其实,仔细观 ...
- gitlab中修改项目名称客户端修改方法
如果gitlab项目名称已经修改,对于本地已经克隆下来的仓库,可以使用如下命令进行修改: git remote set-url origin 新的项目路径
- 微信小程序——扫码后提示“打开失败缺少ID”
解决步骤: 进入通讯录tab->点击右上角添加朋友->搜索框输入:recover,拉到最底下选择小程序进行修复操作 参考:https://developers.weixin.qq.com/ ...
- leecode刷题(17)-- 实现StrStr
leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...