When to use exceptions (was Re: reducing if statements...)

Andrew Dalke dalke at dalkescientific.com
Tue Aug 21 20:43:33 EDT 2001


Christian Tanzer:
> I'd rather use:
>
>    try:
>        somemodule.func(p1, p2, p3)
>    except somemodule.Error_foo, msg :
>        print msg
>        ...
>    except somemodule.Error_bar, msg :
>        ...
>    ### no need for raise anymore

Turns out that in my code that this is rare.  Modules only need
at most a couple of different exceptions.

Plus, there are some times where it's hard to make a subclass
for each exception.  Consider the IOError exceptions raised
by open.  Replacing the errno by a subclass for each value
(and how many values are there?) makes things less readable, IMHO.

But you did say "rather use:", which I'll expand to "rather use,
when it makes sense:" :)

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list