Exception as the primary error handling mechanism?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jan 1 11:26:59 EST 2010


On Fri, 01 Jan 2010 11:02:28 -0500, Benjamin Kaplan wrote:

> I was trying to point out that in
> Python, you don't test errors for your typical conditions, but for ones
> that you know still exist but don't plan on occurring often.

I'm sorry, but that makes no sense to me at all. I don't understand what 
you are trying to say.


You do understand that exceptions aren't just for errors? They are raised 
under specific circumstances. Whether that circumstance is an error or 
not is entirely up to the caller.


try:
    n = mylist.index('fault')
except ValueError:
    print "All is good, no fault detected"
else:
    print "Your data contains a fault in position", n



People get hung up on the idea that exceptions == errors, but they 
aren't. They *may* be errors, and that is one common use, but that 
depends entirely on the caller.


-- 
Steven



More information about the Python-list mailing list