Which exception to use?

Peter Hansen peter at engcorp.com
Tue Jan 28 22:08:33 CET 2003

Chad Netzer wrote:
> Also, Peter Hansen indicated that the following form of exception
> raising is preferred:
>     raise ArgumentError("such-and-such argument must not be negative.")
> I'd be interested to know what the preferred method is (if any).  I
> think raising the class itself is more intuitive to my mind (rather
> than building an instance to raise).  Is there a preferred style?

I'm pretty sure that even your approach is raising an instance, as
the docs say "For class exceptions, this object must be an instance 
of the exception class being raised.".  

I might be wrong, but I believe that both forms will do the same thing,
but in that case mine says more explicitly what is actually happening
so I'd call it "preferred" as a general rule.

I note that both forms are used in the tutorial, and the language
grammar explicitly allows both approaches:

"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."

(Noting that in either case, an *instance* is involved.)


More information about the Python-list mailing list