8. 错误和异常
*************

到目前为止，我们还没有提到错误消息，但是如果你已经尝试过那些例子，你可
能已经看过了一些错误消息。 目前（至少）有两种可区分的错误：*语法错误*
和 *异常*。


8.1. 语法错误
=============

语法错误又称解析错误，可能是你在学习Python 时最容易遇到的错误:

   >>> while True print 'Hello world'
     File "<stdin>", line 1
       while True print 'Hello world'
                      ^
   SyntaxError: invalid syntax

The parser repeats the offending line and displays a little ‘arrow’
pointing at the earliest point in the line where the error was
detected.  The error is caused by (or at least detected at) the token
*preceding* the arrow: in the example, the error is detected at the
keyword "print", since a colon ("':'") is missing before it.  File
name and line number are printed so you know where to look in case the
input came from a script.


8.2. 异常
=========

即使语句或表达式在语法上是正确的，但在尝试执行时，它仍可能会引发错误。
在执行时检测到的错误被称为*异常*，异常不一定会导致严重后果：你将很快学
会如何在Python程序中处理它们。 但是，大多数异常并不会被程序处理，此时
会显示如下所示的错误信息:

   >>> 10 * (1/0)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   ZeroDivisionError: integer division or modulo by zero
   >>> 4 + spam*3
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   NameError: name 'spam' is not defined
   >>> '2' + 2
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: cannot concatenate 'str' and 'int' objects

错误信息的最后一行告诉我们程序遇到了什么类型的错误。异常有不同的类型，
而其类型名称将会作为错误信息的一部分中打印出来：上述示例中的异常类型依
次是："ZeroDivisionError"， "NameError" 和 "TypeError"。作为异常类型打
印的字符串是发生的内置异常的名称。对于所有内置异常都是如此，但对于用户
定义的异常则不一定如此（虽然这是一个有用的规范）。标准的异常类型是内置
的标识符（而不是保留关键字）。

这一行的剩下的部分根据异常类型及其原因提供详细信息。

错误信息的前一部分以堆栈回溯的形式显示发生异常时的上下文。通常它包含列
出源代码行的堆栈回溯；但是它不会显示从标准输入中读取的行。

Built-in Exceptions 列出了内置异常和它们的含义。


8.3. 处理异常
=============

可以编写处理所选异常的程序。请看下面的例子，它会要求用户一直输入，直到
输入的是一个有效的整数，但允许用户中断程序（使用 "Control-C" 或操作系
统支持的其他操作）；请注意用户引起的中断可以通过引发
"KeyboardInterrupt" 异常来指示。:

   >>> while True:
   ...     try:
   ...         x = int(raw_input("Please enter a number: "))
   ...         break
   ...     except ValueError:
   ...         print "Oops!  That was no valid number.  Try again..."
   ...

"try" 语句的工作原理如下。

* 首先，执行 *try 子句* （"try" 和 "except" 关键字之间的（多行）语句
  ） 。

* 如果没有异常发生，则跳过 *except 子句* 并完成 "try" 语句的执行。

* 如果在执行try 子句时发生了异常，则跳过该子句中剩下的部分。然后，如
  果 异常的类型和 "except" 关键字后面的异常匹配，则执行 except 子句 ，
  然 后继续执行 "try" 语句之后的代码。

* 如果发生的异常和 except 子句中指定的异常不匹配，则将其传递到外部的
  "try" 语句中；如果没有找到处理程序，则它是一个 *未处理异常*，执行将
  停止并显示如上所示的消息。

A "try" statement may have more than one except clause, to specify
handlers for different exceptions.  At most one handler will be
executed. Handlers only handle exceptions that occur in the
corresponding try clause, not in other handlers of the same "try"
statement.  An except clause may name multiple exceptions as a
parenthesized tuple, for example:

   ... except (RuntimeError, TypeError, NameError):
   ...     pass

Note that the parentheses around this tuple are required, because
"except ValueError, e:" was the syntax used for what is normally
written as "except ValueError as e:" in modern Python (described
below). The old syntax is still supported for backwards compatibility.
This means "except RuntimeError, TypeError" is not equivalent to
"except (RuntimeError, TypeError):" but to "except RuntimeError as
TypeError:" which is not what you want.

最后的 except 子句可以省略异常名，以用作通配符。但请谨慎使用，因为以这
种方式很容易掩盖真正的编程错误！它还可用于打印错误消息，然后重新引发异
常（同样允许调用者处理异常）:

   import sys

   try:
       f = open('myfile.txt')
       s = f.readline()
       i = int(s.strip())
   except IOError as e:
       print "I/O error({0}): {1}".format(e.errno, e.strerror)
   except ValueError:
       print "Could not convert data to an integer."
   except:
       print "Unexpected error:", sys.exc_info()[0]
       raise

"try" … "except" 语句有一个可选的 *else 子句*，在使用时必须放在所有的
except 子句后面。对于在try 子句不引发异常时必须执行的代码来说很有用。
例如:

   for arg in sys.argv[1:]:
       try:
           f = open(arg, 'r')
       except IOError:
           print 'cannot open', arg
       else:
           print arg, 'has', len(f.readlines()), 'lines'
           f.close()

The use of the "else" clause is better than adding additional code to
the "try" clause because it avoids accidentally catching an exception
that wasn’t raised by the code being protected by the "try" … "except"
statement.

发生异常时，它可能具有关联值，也称为异常 *参数* 。参数的存在和类型取决
于异常类型。

The except clause may specify a variable after the exception name (or
tuple). The variable is bound to an exception instance with the
arguments stored in "instance.args".  For convenience, the exception
instance defines "__str__()" so the arguments can be printed directly
without having to reference ".args".

One may also instantiate an exception first before raising it and add
any attributes to it as desired.

   >>> try:
   ...     raise Exception('spam', 'eggs')
   ... except Exception as inst:
   ...     print type(inst)     # the exception instance
   ...     print inst.args      # arguments stored in .args
   ...     print inst           # __str__ allows args to be printed directly
   ...     x, y = inst.args
   ...     print 'x =', x
   ...     print 'y =', y
   ...
   <type 'exceptions.Exception'>
   ('spam', 'eggs')
   ('spam', 'eggs')
   x = spam
   y = eggs

If an exception has an argument, it is printed as the last part
(‘detail’) of the message for unhandled exceptions.

异常处理程序不仅处理 try 子句中遇到的异常，还处理 try 子句中调用（即使
是间接地）的函数内部发生的异常。例如:

   >>> def this_fails():
   ...     x = 1/0
   ...
   >>> try:
   ...     this_fails()
   ... except ZeroDivisionError as detail:
   ...     print 'Handling run-time error:', detail
   ...
   Handling run-time error: integer division or modulo by zero


8.4. 抛出异常
=============

"raise" 语句允许程序员强制发生指定的异常。例如:

   >>> raise NameError('HiThere')
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   NameError: HiThere

The sole argument to "raise" indicates the exception to be raised.
This must be either an exception instance or an exception class (a
class that derives from "Exception").

如果你需要确定是否引发了异常但不打算处理它，则可以使用更简单的 "raise"
语句形式重新引发异常

   >>> try:
   ...     raise NameError('HiThere')
   ... except NameError:
   ...     print 'An exception flew by!'
   ...     raise
   ...
   An exception flew by!
   Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
   NameError: HiThere


8.5. 用户自定义异常
===================

Programs may name their own exceptions by creating a new exception
class (see 类 for more about Python classes).  Exceptions should
typically be derived from the "Exception" class, either directly or
indirectly.  For example:

   >>> class MyError(Exception):
   ...     def __init__(self, value):
   ...         self.value = value
   ...     def __str__(self):
   ...         return repr(self.value)
   ...
   >>> try:
   ...     raise MyError(2*2)
   ... except MyError as e:
   ...     print 'My exception occurred, value:', e.value
   ...
   My exception occurred, value: 4
   >>> raise MyError('oops!')
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   __main__.MyError: 'oops!'

In this example, the default "__init__()" of "Exception" has been
overridden.  The new behavior simply creates the *value* attribute.
This replaces the default behavior of creating the *args* attribute.

可以定义异常类，它可以执行任何其他类可以执行的任何操作，但通常保持简单
，通常只提供许多属性，这些属性允许处理程序为异常提取有关错误的信息。在
创建可能引发多个不同错误的模块时，通常的做法是为该模块定义的异常创建基
类，并为不同错误条件创建特定异常类的子类:

   class Error(Exception):
       """Base class for exceptions in this module."""
       pass

   class InputError(Error):
       """Exception raised for errors in the input.

       Attributes:
           expr -- input expression in which the error occurred
           msg  -- explanation of the error
       """

       def __init__(self, expr, msg):
           self.expr = expr
           self.msg = msg

   class TransitionError(Error):
       """Raised when an operation attempts a state transition that's not
       allowed.

       Attributes:
           prev -- state at beginning of transition
           next -- attempted new state
           msg  -- explanation of why the specific transition is not allowed
       """

       def __init__(self, prev, next, msg):
           self.prev = prev
           self.next = next
           self.msg = msg

大多数异常都定义为名称以“Error”结尾，类似于标准异常的命名。

许多标准模块定义了它们自己的异常，以报告它们定义的函数中可能出现的错误
。有关类的更多信息，请参见类 类。


8.6. 定义清理操作
=================

"try" 语句有另一个可选子句，用于定义必须在所有情况下执行的清理操作。例
如:

   >>> try:
   ...     raise KeyboardInterrupt
   ... finally:
   ...     print 'Goodbye, world!'
   ...
   Goodbye, world!
   Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
   KeyboardInterrupt

A *finally clause* is always executed before leaving the "try"
statement, whether an exception has occurred or not. When an exception
has occurred in the "try" clause and has not been handled by an
"except" clause (or it has occurred in an "except" or "else" clause),
it is re-raised after the "finally" clause has been executed.  The
"finally" clause is also executed “on the way out” when any other
clause of the "try" statement is left via a "break", "continue" or
"return" statement.  A more complicated example (having "except" and
"finally" clauses in the same "try" statement works as of Python 2.5):

   >>> def divide(x, y):
   ...     try:
   ...         result = x / y
   ...     except ZeroDivisionError:
   ...         print "division by zero!"
   ...     else:
   ...         print "result is", result
   ...     finally:
   ...         print "executing finally clause"
   ...
   >>> divide(2, 1)
   result is 2
   executing finally clause
   >>> divide(2, 0)
   division by zero!
   executing finally clause
   >>> divide("2", "1")
   executing finally clause
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "<stdin>", line 3, in divide
   TypeError: unsupported operand type(s) for /: 'str' and 'str'

As you can see, the "finally" clause is executed in any event.  The
"TypeError" raised by dividing two strings is not handled by the
"except" clause and therefore re-raised after the "finally" clause has
been executed.

在实际应用程序中，"finally" 子句对于释放外部资源（例如文件或者网络连接
）非常有用，无论是否成功使用资源。


8.7. 预定义的清理操作
=====================

某些对象定义了在不再需要该对象时要执行的标准清理操作，无论使用该对象的
操作是成功还是失败。请查看下面的示例，它尝试打开一个文件并把其内容打印
到屏幕上。:

   for line in open("myfile.txt"):
       print line,

The problem with this code is that it leaves the file open for an
indeterminate amount of time after the code has finished executing.
This is not an issue in simple scripts, but can be a problem for
larger applications. The "with" statement allows objects like files to
be used in a way that ensures they are always cleaned up promptly and
correctly.

   with open("myfile.txt") as f:
       for line in f:
           print line,

After the statement is executed, the file *f* is always closed, even
if a problem was encountered while processing the lines. Other objects
which provide predefined clean-up actions will indicate this in their
documentation.
