Commit a2a9888f authored by Michael W. Hudson's avatar Michael W. Hudson

Updates to the exceptions documentation (this is my patch #1156102).

parent c72dd38f
...@@ -182,16 +182,20 @@ either case, it prints a stack backtrace, except when the exception is ...@@ -182,16 +182,20 @@ either case, it prints a stack backtrace, except when the exception is
\exception{SystemExit}\withsubitem{(built-in \exception{SystemExit}\withsubitem{(built-in
exception)}{\ttindex{SystemExit}}. exception)}{\ttindex{SystemExit}}.
Exceptions are identified by class instances. Exceptions are identified by class instances. The \keyword{except}
Selection of a matching except clause is based on object identity. clause is selected depending on the class of the instance: it must
The \keyword{except} clause must reference the same class or a base reference the class of the instance or a base class thereof. The
class of it. instance can be received by the handler and can carry additional
information about the exceptional condition.
When an exception is raised, an object (maybe \code{None}) is passed
as the exception's \emph{value}; this object does not affect the Exceptions can also be identified by strings, in which case the
selection of an exception handler, but is passed to the selected \keyword{except} clause is selected by object identity. An arbitrary
exception handler as additional information. For class exceptions, value can be raised along with the identifying string which can be
this object must be an instance of the exception class being raised. passed to the handler.
\deprecated{2.5}{String exceptions should not be used in new code.
They will not be supported in a future version of Python. Old code
should be rewritten to use class exceptions instead.}
\begin{notice}[warning] \begin{notice}[warning]
Messages to exceptions are not part of the Python API. Their contents may Messages to exceptions are not part of the Python API. Their contents may
......
...@@ -223,11 +223,11 @@ clause, if present, must be last; it matches any exception. For an ...@@ -223,11 +223,11 @@ clause, if present, must be last; it matches any exception. For an
except clause with an expression, that expression is evaluated, and the except clause with an expression, that expression is evaluated, and the
clause matches the exception if the resulting object is ``compatible'' clause matches the exception if the resulting object is ``compatible''
with the exception. An object is compatible with an exception if it with the exception. An object is compatible with an exception if it
is either the object that identifies the exception, or (for exceptions is the class or a base class of the exception object, a tuple
that are classes) it is a base class of the exception, or it is a containing an item compatible with the exception, or, in the
tuple containing an item that is compatible with the exception. Note (deprecated) case of string exceptions, is the raised string itself
that the object identities must match, i.e. it must be the same (note that the object identities must match, i.e. it must be the same
object, not just an object with the same value. string object, not just a string with the same value).
\kwindex{except} \kwindex{except}
If no except clause matches the exception, the search for an exception If no except clause matches the exception, the search for an exception
...@@ -239,14 +239,14 @@ and a search starts for the new exception in the surrounding code and ...@@ -239,14 +239,14 @@ and a search starts for the new exception in the surrounding code and
on the call stack (it is treated as if the entire \keyword{try} statement on the call stack (it is treated as if the entire \keyword{try} statement
raised the exception). raised the exception).
When a matching except clause is found, the exception's parameter is When a matching except clause is found, the exception is assigned to
assigned to the target specified in that except clause, if present, the target specified in that except clause, if present, and the except
and the except clause's suite is executed. All except clauses must clause's suite is executed. All except clauses must have an
have an executable block. When the end of this block executable block. When the end of this block is reached, execution
is reached, execution continues normally after the entire try continues normally after the entire try statement. (This means that
statement. (This means that if two nested handlers exist for the same if two nested handlers exist for the same exception, and the exception
exception, and the exception occurs in the try clause of the inner occurs in the try clause of the inner handler, the outer handler will
handler, the outer handler will not handle the exception.) not handle the exception.)
Before an except clause's suite is executed, details about the Before an except clause's suite is executed, details about the
exception are assigned to three variables in the exception are assigned to three variables in the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment