Re: is there any overheard with try/except statements?



In article <EfSdnS4P5s1475LZnZ2dnUVZ_vydnZ2d@xxxxxxx>,
John Salerno <johnjsal@xxxxxxxxxxxxxxx> wrote:

One of the things I learned with C# is that it's always better to handle
any errors that might occur within the codes itself (i.e. using if
statements, etc. to catch potential out of range indexing) rather than
use too many try/catch statements, because there is some overhead every
time the program encounters the try.

Is this the case at all with Python, in terms of extra work or slower
speed? Or is try/except implemented differently in Python than it is in
other languages, so that it runs just like any other code?

Thanks.

Repeat after me: "Python is not <fill in name of your favorite language>"

In C++ (and, I assume, C#), exception handling is relatively expensive, and
is avoided for this reason. It's also avoided (in C++, anyway) because
it's only in the past few (5-10) years that you could pretty much count on
whatever compiler you were using implementing them correctly. Lastly, many
C++/C# programmers came from C, where exceptions don't exist, so they're
not used to using them.

But, that's not Python. In Python, exceptions work as advertised, and
they're cheap. The pythonic way to do things is to embrace the maxim that
"it's easier to ask forgivness than to ask permission". It is usually
cleaner and faster to write a try block than an if statement. For example,
I'll write:

try:
x = foo[y]
except IndexError:
blah

instead of

if y < len (foo):
x = foo[y]
else:
blah

every time.
.



Relevant Pages

  • Re: Should I use "if" or "try" (as a matter of speed)?
    ... Joel Spolsky might be a great C++ programmer, ... arguments about exceptions do not hold in Python. ...
    (comp.lang.python)
  • Re: Exception handling questions
    ... "Gonçalo Rodrigues" wrote in message ... > I've got two somewhat general questions related to exception handling ... > Now in Python, being a dynamically typed language, everything is ... > using exceptions. ...
    (comp.lang.cpp)
  • python-dev Summary for 2004-09-01 through 2004-09-15
    ... The in-development version of the documentation for Python can be found at ... Movement in PEP Land ... Contributing threads: ... The idea of reorganizing the exceptions hierarchy came up again (see ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... > Maybe we should just implement floats in Python. ... Your platform C may or may not, and your OS may or may not. ... >> I believe Python should raise exceptions in these cases by default, ... trying to _increase_ libm error coverage, it was trying to get back ...
    (comp.lang.python)
  • Re: Should I use "if" or "try" (as a matter of speed)?
    ... I dutifully evangelize on the goodness of Python whenever I talk with fellow developers, but I always hit a snag when it comes to discussing the finer points of the execution model (specifically, exceptions). ... of writing it that I've ever found is to encapsulate the raise statement ...
    (comp.lang.python)

Loading