raise_stmt |
::= | "raise" [expression ["," expression
["," expression]]] |
If no expressions are present, raise re-raises the last expression that was active in the current scope. If no exception is active in the current scope, an exception is raised indicating this error.
如果不给出表达式(expressions),raise重新引发当前范围内上次引发的表达式。
Otherwise, raise evaluates the expressions to get three
objects, using None
as the value of omitted expressions. The
first two objects are used to determine the type and
value of the exception.
否则,raise对其后的第一个表达式求值,该值必须给出字符串,类,或者实例对象。如果还有第二个表达式,也被求值,否则以None代之。如果第一表达式是一个类对象,那么第二表达式可以是该类或者其衍生类的一个实例,然后该实例就被引发。如果第二表达式不是这样的实例,那么给出的类就会被实例化。实例化的参量列表按下列方式决定:如果第二表达式是一元组,就用来当作参量列表;如果是None,参量列表就为空;否则,参量列表仅包含单个参量,就是第二个表达式。如果第一表达式是一个实例对象,第二表达式必须是None。
If the first object is an instance, the type of the exception is the
class of the instance, the instance itself is the value, and the
second object must be None
.
如果第一对象是一个字符串,它就引发一个由第一对象标示的,以第二对象(或None)为参数的异常。如果第一对象是一个类或者实例,就会引发由前述所决定的实例的类所标示的,以该实例为参数的异常。
If the first object is a class, it becomes the type of the exception.
The second object is used to determine the exception value: If it is
an instance of the class, the instance becomes the exception value.
If the second object is a tuple, it is used as the argument list for
the class constructor; if it is None
, an empty argument list is
used, and any other object is treated as a single argument to the
constructor. The instance so created by calling the constructor is
used as the exception value.
If a third object is present and not None
, it must be a
traceback object (see section 3.2), and
it is substituted instead of the current location as the place where
the exception occurred. If the third object is present and not a
traceback object or None
, a TypeError exception is
raised. The three-expression form of raise is useful to
re-raise an exception transparently in an except clause, but
raise with no expressions should be preferred if the
exception to be re-raised was the most recently active exception in
the current scope.
如果给出了第三对象,且它不是None,它就应该为一个回溯对象(见3.2节),且用其替代当前位置作为异常发生的地点。这可用于透明地在异常子句中重新引发一个异常。
Additional information on exceptions can be found in section 4.2, and information about handling exceptions is in section 7.4.