subclassing Exceptions

phawkins at spamnotconnact.com phawkins at spamnotconnact.com
Tue Jul 24 12:37:13 EDT 2001


>>>>> "GE" == Greg Ewing <greg at cosc.canterbury.ac.nz> writes:

GE> phawkins at spamnotconnact.com wrote:
>> 
>> >>> raise strExceptionWithArg, "no thanks"

GE> Shouldn't that be

GE>    raise strExceptionWithArg("no thanks")

GE> ?

GE> According to my reading of the docs, the first form
GE> is illegal, since if the exception is a class and a 
GE> value is supplied it is supposed to be a subclass of the
GE> exception class.

Nope.

The applicable phrase in the doc, below, is: "otherwise, the argument
list consists of a single argument which is the second expression"

___
"raise_stmt:     "raise" [expression ["," expression ["," expression]]]
If no expressions are present, raise re-raises the last
expression that was raised in the current scope.

Otherwise, raise evaluates its first expression, which must yield
a string, class, or instance object.  If there is a second expression,
this is evaluated, else None is substituted.  If the first
expression is a class object, then the second expression may be an
instance of that class or one of its derivatives, and then that
instance is raised.  If the second expression is not such an instance,
the given class is instantiated.  The argument list for the
instantiation is determined as follows: if the second expression is a
tuple, it is used as the argument list; if it is None, the
argument list is empty; otherwise, the argument list consists of a
single argument which is the second expression.  If the first
expression is an instance object, the second expression must be 
None."
 
http://www.python.org/doc/current/ref/raise.html#raise

-- 
Patricia J. Hawkins
Hawkins Internet Applications, LLC





More information about the Python-list mailing list