User-defined exceptions from 2.6

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Feb 1 13:45:53 CET 2010


On Mon, 01 Feb 2010 02:19:39 -0800, Joan Miller wrote:

> Which is the best way to create user-defined exceptions since that
> *BaseException.message* is deprecated in Python 2.6 ?


Inherit from an existing exception.


>>> class MyValueException(ValueError):
...     pass
...
>>>
>>> raise MyValueException("value is impossible, try again")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.MyValueException: value is impossible, try again



>>> class GenericError(Exception):
...     pass
...
>>> raise GenericError("oops")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.GenericError: oops


Do you need anything more complicated?


-- 
Steven



More information about the Python-list mailing list