Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81932e2

Browse files
committed
Try to improve the explanation of the "raise" statement and how its arguments
are interpreted. This closes SF bug #532467.
1 parent 5d09904 commit 81932e2

2 files changed

Lines changed: 39 additions & 33 deletions

File tree

Doc/ref/ref4.tex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ \section{Exceptions \label{exceptions}}
192192
class of it.
193193

194194
When an exception is raised, an object (maybe \code{None}) is passed
195-
as the exception's ``parameter'' or ``value''; this object does not
196-
affect the selection of an exception handler, but is passed to the
197-
selected exception handler as additional information. For class
198-
exceptions, this object must be an instance of the exception class
199-
being raised.
195+
as the exception's \emph{value}; this object does not affect the
196+
selection of an exception handler, but is passed to the selected
197+
exception handler as additional information. For class exceptions,
198+
this object must be an instance of the exception class being raised.
200199

201200
See also the description of the \keyword{try} statement in section
202201
\ref{try} and \keyword{raise} statement in section \ref{raise}.

Doc/ref/ref6.tex

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -507,37 +507,44 @@ \section{The \keyword{raise} statement \label{raise}}
507507
\end{productionlist}
508508
509509
If no expressions are present, \keyword{raise} re-raises the last
510-
expression that was raised in the current scope.
511-
512-
Otherwise, \keyword{raise} evaluates its first expression, which must yield
513-
a string, class, or instance object. If there is a second expression,
514-
this is evaluated, else \code{None} is substituted. If the first
515-
expression is a class object, then the second expression may be an
516-
instance of that class or one of its derivatives, and then that
517-
instance is raised. If the second expression is not such an instance,
518-
the given class is instantiated. The argument list for the
519-
instantiation is determined as follows: if the second expression is a
520-
tuple, it is used as the argument list; if it is \code{None}, the
521-
argument list is empty; otherwise, the argument list consists of a
522-
single argument which is the second expression. If the first
523-
expression is an instance object, the second expression must be
524-
\code{None}.
510+
expression that was active in the current scope. If no exception has
511+
been active in the current scope, an exception is raised that
512+
indicates indicates that this is the error.
525513
\index{exception}
526514
\indexii{raising}{exception}
527515
528-
If the first object is a string, it then raises the exception
529-
identified by the first object, with the second one (or \code{None})
530-
as its parameter. If the first object is a class or instance,
531-
it raises the exception identified by the class of the instance
532-
determined in the previous step, with the instance as
533-
its parameter.
534-
535-
If a third object is present, and it is not \code{None}, it should be
536-
a traceback object (see section~\ref{traceback}), and it is
537-
substituted instead of the current location as the place where the
538-
exception occurred. This is useful to re-raise an exception
539-
transparently in an except clause.
540-
\obindex{traceback}
516+
Otherwise, \keyword{raise} evaluates the expressions to get three
517+
objects, using \code{None} as the value of omitted expressions. The
518+
first two objects are used to determine the \emph{type} and
519+
\emph{value} of the exception.
520+
521+
If the first object is an instance, the type of the exception is the
522+
class of the instance, the instance itself if the value, and the
523+
second object must be \code{None}.
524+
525+
If the first object is a class, it becomes the type of the exception.
526+
The second object is used to determine the exception value: If it is
527+
an instance of the class, the instance becomes the exception value.
528+
If the second object is a tuple, it is used as the argument list for
529+
the class constructor; if it is \code{None}, an empty argument list is
530+
used, and any other object is treated as a single argument to the
531+
constructor. The instance so created by calling the constructor is
532+
used as the exception value.
533+
534+
If the first object is a string, the string object is the exception
535+
type, and the second object becomes the exception value.
536+
537+
If a third object is present and not \code{None}, it must be a
538+
traceback\obindex{traceback} object (see section~\ref{traceback}), and
539+
it is substituted instead of the current location as the place where
540+
the exception occurred. If the third object is present and not a
541+
traceback object or \code{None}, a \exception{TypeError} exception is
542+
raised. The three-expression form of \keyword{raise} is useful to
543+
re-raise an exception transparently in an except clause, but
544+
\keyword{raise} with no expressions should be preferred if the
545+
exception to be re-raised was the most recently active exception in
546+
the current scope.
547+
541548
542549
543550
\section{The \keyword{break} statement \label{break}}

0 commit comments

Comments
 (0)