Exception as the primary error handling mechanism?

Stephen Hansen apt.shansen at gmail.com
Sat Jan 2 03:05:56 EST 2010


On Fri, Jan 1, 2010 at 5:36 PM, Peng Yu <pengyu.ut at gmail.com> wrote:

> >> Otherwise, could some python expert explain to me why exception is
> >> widely used for error handling in python? Is it because the efficiency
> >> is not the primary goal of python?
> >
> > Correct; programmer efficiency is a more important goal for Python
> instead.
> > Python is ~60-100x slower than C;[1] if someone is worried by the
> > inefficiency caused by exceptions, then they're using completely the
> > wrong language.
>
> Could somebody let me know how the python calls and exceptions are
> dispatched? Is there a reference for it?
>

I don't quite understand what you're asking here, but it sounds almost like
you're looking at the question from an incorrect POV. "Exceptions" are a
general sort of concept in computer science and various computer programming
languages, but they are not at all equal from one language to another. The
document you referenced was speaking to a particular implementation of the
concept, and speaking to particular characteristics of that language's
implementation. Even though its not talking just about say, C, C#, Java, or
anything -- its speaking from a certain POV of a certain classes of
languages.

In Python, setting up an exception -- the 'try' clause -- costs virtually
nothing. Its about equivalent to having a 'pass' statement in there. If you
do a test for every iteration of some activity, you're incurring a
non-negligable cost each time. If you're performing an action and "usually"
(to varying definitions of 'usually'), it's going to succeed-- then that
test will result in far more cost in time then using a try/except clause in
Python.

Because in the implementation of exceptions in Python, you only pay a more
expensive cost /if/ that exception is thrown and handled. If its very likely
that in a situation an exception would be thrown, then yes-- then you should
probably test first... if that exception-catch is so expensive as to be
important to your. In most cases, its not. In the vast majority of cases,
this is premature optimization and often adds additional weight to the test
as you have to protect against race conditions. (As an aside, in many cases
using exceptions actually helps you in a wider problem of preventing race
conditions. Its not a cure-all by any means, but it helps)

If someone specifies a file, the chances are-- the file is there. Its
cheaper for you to just try to open it then to test if its there first,
because if you do the test? Then the likely circumstance (the file exists)
is running code to test that case /every time/. Whereas if you just try to
open it, in the likely circumstance -- it works. In the exceptional
circumstance, it might cost more then if you had tested first... but that's
an exceptional circumstance and therefore more rare.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100102/1ed4aa14/attachment.html>


More information about the Python-list mailing list