Recommended exception method for modules?
Fredrik Lundh
effbot at telia.com
Thu Oct 5 11:37:59 EDT 2000
Grant Edwards wrote:
> 3) a class for each type of error with value(s) that further
> explain what is wrong:
>
> class PosixSerialBaudError(Exception):
> [...]
>
> if not validBaud(r):
> raise PosixSerialBaudError(r)
>
> [this last one could be done as a subclass of a general
> PosixSerialError class, -- that way somebody can catch
> either all of my exceptions or just individual ones.
> Right?]
> Does anybody care to share their opinion on the relative merits
> of these?
Don't use string exceptions. They're evil. Enough said.
For simple cases, reuse an existing exception (if reasonable),
or use a single exception class, either inherited from Exception
or from a suitable standard exception class (alternative 2).
(in 2.0, the -X option is gone, and built-in exceptions are
always classes)
When it makes sense, use an exception hierarchy (alternative 3).
You can always move from (2) to (3), without breaking existing
code.
</F>
More information about the Python-list
mailing list