First release of Shed Skin, a Python-to-C++ compiler.
Brian Quinlan
brian at sweetapp.com
Tue Sep 13 06:04:36 EDT 2005
Mark Dufour wrote:
> You're right, I don't feel safe about that. It's a bad example. I just
> prefer error codes, because the code usually becomes cleaner (at least
> to me). I would really like it if I could do something like this:
>
> f = file(name)
> if not f:
> print 'error opening file %s: %s', name, str(f.error)
> sys.exit()
I think you meant:
print 'error opening file %s: %s' % (name, str(f.error))
You forgot to check for an error when:
o when you wrote f.error [attribute "error" might not exist e.g. f is
None]
o you called str(f.error) [might contain unicode characters that can't
be converted to a string using the default
encoding]
o when you used the % operator [format string might be wrong]
And, of course, pretty much every expression can result in a memory error.
Exception handling is a boon because almost EVERY expression that you
write can result in a error and checking each one is tedious in the
extreme. So people forget and then their programs exhibit very odd
behavior that is very difficult to debug.
If you look at the Python C source, you'll notice that probably 50% of
the code is devoted to error handling (that was a guess).
Cheers,
Brian
More information about the Python-list
mailing list