理解Python语言里的异常(Exception)

Exception is as a sort of structured "super go to".
异常是一种结构化的"超级goto".

作为一个数十年如一日地钟爱C语言的程序员(因为C程序员需要记忆的关键字很少,而且可以很惬意地玩内存),对于高级语言如Python里的异常(Exception)一直不甚理解,尤其是其实现机理。但读了《Learning Python》一书中上面这句话(尤其是goto关键字)后,忽然豁然开朗。

如果用C语言编写一个鲁棒性良好的程序,一般得在每一个可能出错的运算之后检查返回值或状态码,然后在程序执行的时候根据返回值或状态做出不同的处理。例如:

 1 int doStuff()
 2 {                                       /* C program                */
 3         if (doFirstThing() == ERROR)    /* Detect errors everywhere */
 4                 return ERROR;           /* even if not handled here */
 5         if (doNextThing() == ERROR)
 6                 return ERROR;
 7         ...
 8         return doLastThing();
 9 }
10 
11 int main()
12 {
13         if (doStuff() == ERROR)
14                 badEnding();
15         else
16                 goodEnding();
17         ...
18 }

实际上,在现实的C程序中,通常用于处理错误检查和用于实际工作的代码数量相当。 但是, 在Python中,程序员就不用那么小心翼翼和神经质了。你可以把程序的任意片段包装在异常处理器内,然后编写从事实际工作的部分,假设一切都工作正常。 例如:

 1 def doStuff():                  # Python code
 2         doFirstThing()          # We don't care about exceptions here,
 3         doNextThing()           # so we don't need to detect them
 4         ...
 5         doLastThing()
 6 
 7 if __name__ == '__main__':
 8         try:
 9                 doStuff()       # This is where we care about results,
10         except:                 # so it's the only place we must check
11                 badEnding()
12         else:
13                 goodEnding()
14         ...

在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.

简而言之,异常让我们从一个程序中任意大的代码块中跳将出来。

《理解Python语言里的异常(Exception)》

2. Exception Roles 异常充当的最常见的几种角色

  • Error handling 错误处理
  • Event notification 事件通知
  • Special-case handling 特殊情况处理
  • Termination actions 行为终止
  • Unusual control flows 非常规控制流

《理解Python语言里的异常(Exception)》

《理解Python语言里的异常(Exception)》

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)

StatementMeaning
try/except Catch and recover from exceptions raised by Python, or by you.
try/finallyPerform cleanup actions, whether exceptions occur or not.
raiseTrigger an exception manually in your code.
assertConditionally trigger an exception in your code.
with/asImplement context managers in Python 2.6+.

参考资料:

1. Book: Learning Python, Fourth Edition : PART VII Exceptions and Tools

    原文作者:veli
    原文地址: https://www.cnblogs.com/idorax/p/6730763.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注