7.4 try语句 The try statement

The try statement specifies exception handlers and/or cleanup code for a group of statements:

try语句为一组语句指定异常处理器和/或清除代码:

try_stmt  ::=  try_exc_stmt | try_fin_stmt
try_exc_stmt  ::=  "try" ":" suite
    ("except" [expression ["," target]] ":" suite)+
    ["else" ":" suite]
try_fin_stmt  ::=  "try" ":" suite "finally" ":" suite
Download entire grammar as text.

There are two forms of try statement: try...except and try...finally. These forms cannot be mixed (but they can be nested in each other).

有两种形式的try语句: try...except 和 try...finally. 它们不能混合使用(但它们可以互相嵌套).

The try...except form specifies one or more exception handlers (the except clauses). When no exception occurs in the try clause, no exception handler is executed. When an exception occurs in the try suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is ``compatible'' with the exception. An object is compatible with an exception if it is either the object that identifies the exception, or (for exceptions that are classes) it is a base class of the exception, or it is a tuple containing an item that is compatible with the exception. Note that the object identities must match, i.e. it must be the same object, not just an object with the same value.

try...except形式指定一个或多个异常处理器(异常子句). 当在try子句中没有异常发生时, 异常处理器将不被执行. 当在try子句中有异常发生时, 就会开始搜索异常处理器.它会按顺序搜索直到第一个匹配的处理器找到为止. 如果存在一个没有指定异常的except语句, 它必须放在最后, 它会匹配任何异常.当一个except匹配, 相应表达式会被计算. 如果结果对象与该异常"兼容", 那么该子句就匹配了这个异常.如果这个对象是标识这个异常的对象, 或(异常类)是该异常的基类, 或者它是一个包括与该异常兼容的对象的元组就称为这个对象是兼容的. 注意对象的标识必须匹配, 那就是说, 它必须是相同的对象, 不仅是具有相同值的对象.

If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack.

如果没有except子句匹配异常, 异常处理器的搜索工作将继续在调用栈的外层代码中进行.

If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire try statement raised the exception).

如果在except子句头部计算表达式时就引发了异常, 原来的异常处理器搜索工作就中断, 并在外层代码搜索新的异常处理器(就好像处理整个try语句发生了异常一样).

When a matching except clause is found, the exception's parameter is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.)

当找到了一个匹配的except子句时, 异常的参数被赋给了except子句中指定的对象(如果给出), 并且执行其后的语句序列. 所有的except子句必须一个可执行代码块.当执行该代码块末尾时, 会转到整个try语句之后继续正常执行(这意味着, 对于一个异常如果有两个嵌套的异常处理器, 并且异常由内层的处理器处理, 那么外层处理器就不会响应这个异常).

Before an except clause's suite is executed, details about the exception are assigned to three variables in the sysmodule: sys.exc_type receives the object identifying the exception; sys.exc_value receives the exception's parameter; sys.exc_traceback receives a traceback object (see section 3.2) identifying the point in the program where the exception occurred. These details are also available through the sys.exc_info() function, which returns a tuple (exc_type, exc_value, exc_traceback). Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception.

在某个except子句的语句序列被执行前, 会将这个异常的详细情况记录在sys模块的三个变量中:sys.exc_type记录标识异常的对象;sys.exc_vaule记录异常的参数; sys.exc_traceback记录标识程序中异常发生点的跟踪回溯对象(见3.2节).这些信息可以通过函数sys.exc_info()得到, 它会返回一个元组(exc_type, exc_value, exc_traceback). 两者比较而言应该使用函数方法, 因为在线程式化程序使用变量是不安全的. 自Python1.5开始, 这些变量在从返回发生异常的函数时会恢复它们之前的值.

The optional else clause is executed if and when control flows off the end of the try clause.7.1 Exceptions in the else clause are not handled by the preceding except clauses.

当控制从try子句尾部中结束时, 就执行可选的else子句. 7.2 在else子句中引发的异常不会在前面的except子句得到处理.

The try...finally form specifies a `cleanup' handler. The try clause is executed. When no exception occurs, the finally clause is executed. When an exception occurs in the try clause, the exception is temporarily saved, the finally clause is executed, and then the saved exception is re-raised. If the finally clause raises another exception or executes a return or break statement, the saved exception is lost. A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation - this restriction may be lifted in the future). The exception information is not available to the program during execution of the finally clause.

try...finally形式指定一个清除处理器. 在执行try语句块没有异常发生时, finally子句被执行.在异常引发时, 该异常就被临时保存起来, finally也被执行, 然后暂存的异常被重新引发.如果执行finally子句时引发了另一个异常或执行了return或break语句, 就会抛弃保存的异常,在finally子句中的continue语句是非法的(这么做的原因是当前实现的原因 -- 这个限制可能也会保留下去)在执行finally子句时异常信息是无效的.

When a return, break or continue statement is executed in the try suite of a try...finally statement, the finally clause is also executed `on the way out.' A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation -- this restriction may be lifted in the future).

当在try...finally语句中的try语句序列的return, break 或continue执行后,finally子句也会执行.在finally子句中的continue语句是非法的(这么做的原因是当前实现的原因 -- 这个限制可能也会保留下去)

Additional information on exceptions can be found in section 4.2, and information on using the raise statement to generate exceptions may be found in section 6.9.



注脚

... clause.7.1
Currently, control ``flows off the end'' except in the case of an exception or the execution of a return, continue, or break statement.
... 就执行可选的else子句.7.2
现在, "控制从try子句尾部中结束"除了出现异常情况和执行中有return, continue, 或break语句的时候.