User-defined exceptions from 2.6

Joan Miller peloko45 at gmail.com
Mon Feb 1 07:59:03 EST 2010


On 1 feb, 12:45, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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?
>
It's enought, thanks!



More information about the Python-list mailing list