Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Exception as the primary error handling mechanism? (http://www.velocityreviews.com/forums/t710399-exception-as-the-primary-error-handling-mechanism.html)

Peng Yu 01-01-2010 04:47 AM

Exception as the primary error handling mechanism?
 
I observe that python library primarily use exception for error
handling rather than use error code.

In the article API Design Matters by Michi Henning

Communications of the ACM
Vol. 52 No. 5, Pages 46-56
10.1145/1506409.1506424
http://cacm.acm.org/magazines/2009/5...tters/fulltext

It says "Another popular design flaw—namely, throwing exceptions for
expected outcomes—also causes inefficiencies because catching and
handling exceptions is almost always slower than testing a return
value."

My observation is contradicted to the above statement by Henning. If
my observation is wrong, please just ignore my question below.

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?

Chris Rebert 01-01-2010 05:24 AM

Re: Exception as the primary error handling mechanism?
 
On Thu, Dec 31, 2009 at 8:47 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
> I observe that python library primarily use exception for error
> handling rather than use error code.
>
> In the article API Design Matters by Michi Henning
>
> Communications of the ACM
> Vol. 52 No. 5, Pages 46-56
> 10.1145/1506409.1506424
> http://cacm.acm.org/magazines/2009/5...tters/fulltext
>
> It says "Another popular design flaw—namely, throwing exceptions for
> expected outcomes—also causes inefficiencies because catching and
> handling exceptions is almost always slower than testing a return
> value."
>
> My observation is contradicted to the above statement by Henning. If
> my observation is wrong, please just ignore my question below.
>
> 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.

Cheers,
Chris
--
http://blog.rebertia.com

[1] http://shootout.alioth.debian.org/u6...=on&calc=chart

Benjamin Kaplan 01-01-2010 05:26 AM

Re: Exception as the primary error handling mechanism?
 
On Thu, Dec 31, 2009 at 11:47 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
> I observe that python library primarily use exception for error
> handling rather than use error code.
>
> In the article API Design Matters by Michi Henning
>
> Communications of the ACM
> Vol. 52 No. 5, Pages 46-56
> 10.1145/1506409.1506424
> http://cacm.acm.org/magazines/2009/5...tters/fulltext
>
> It says "Another popular design flaw—namely, throwing exceptions for
> expected outcomes—also causes inefficiencies because catching and
> handling exceptions is almost always slower than testing a return
> value."
>
> My observation is contradicted to the above statement by Henning. If
> my observation is wrong, please just ignore my question below.
>
> 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?
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Read the quote again "Another popular design flaw—namely, throwing
exceptions *for expected outcomes*"
In Python, throwing exceptions for expected outcomes is considered
very bad form (well, except for StopIteration but that should almost
never be handled directly by the programmer).

To answer why people recommend using "Easier to Ask Forgiveness than
Permission" as opposed to "Look Before You Leap" : Because of the way
it's implemented, Python works quite differently from most languages.
An attribute look-up is rather expensive because it's a hash table
look-up at run time. Wrapping a small piece of code in a try block
however, isn't (relatively) very expensive at all in Python. It's only
catching the exception that's expensive, but if you're catching the
exception, something has gone wrong anyway and performance isn't your
biggest issue.

Steven D'Aprano 01-01-2010 08:26 AM

Re: Exception as the primary error handling mechanism?
 
On Thu, 31 Dec 2009 20:47:49 -0800, Peng Yu wrote:

> I observe that python library primarily use exception for error handling
> rather than use error code.
>
> In the article API Design Matters by Michi Henning
>
> Communications of the ACM
> Vol. 52 No. 5, Pages 46-56
> 10.1145/1506409.1506424
> http://cacm.acm.org/magazines/2009/5...tters/fulltext
>
> It says "Another popular design flaw—namely, throwing exceptions for
> expected outcomes—also causes inefficiencies because catching and
> handling exceptions is almost always slower than testing a return
> value."


This is very, very wrong.

Firstly, notice that the author doesn't compare the same thing. He
compares "catching AND HANDLING" the exception (emphasis added) with
*only* testing a return value. Of course it is faster to test a value and
do nothing, than it is to catch an exception and then handle the
exception. That's an unfair comparison, and that alone shows that the
author is biased against exceptions.

But it's also wrong. If you call a function one million times, and catch
an exception ONCE (because exceptions are rare) that is likely to be
much, much faster than testing a return code one million times.

Before you can talk about which strategy is faster, you need to
understand your problem. When exceptions are rare (in CPython, about one
in ten or rarer) then try...except is faster than testing each time. The
exact cut-off depends on how expensive the test is, and how much work
gets done before the exception is raised. Using exceptions is only slow
if they are common.

But the most important reason for preferring exceptions is that the
alternatives are error-prone! Testing error codes is the anti-pattern,
not catching exceptions.

See, for example:

http://c2.com/cgi/wiki?UseExceptions...dOfErrorValues
http://c2.com/cgi/wiki?ExceptionsAreOurFriends
http://c2.com/cgi/wiki?AvoidExceptionsWheneverPossible

Despite the title of that last page, it has many excellent arguments for
why exceptions are better than the alternatives.

(Be warned: the c2 wiki is filled with Java and C++ programmers who
mistake the work-arounds for quirks of their language as general design
principles. For example, because exceptions in Java are evcen more
expensive and slow than in Python, you will find lots of Java coders
saying "don't use exceptions" instead of "don't use exceptions IN JAVA".)

There are many problems with using error codes:

* They complicate your code. Instead of returning the result you care
about, you have to return a status code and the return result you care
about. Even worse is to have a single global variable to hold the status
of the last function call!

* Nobody can agree whether the status code means the function call
failed, or the function call succeeded.

* If the function call failed, what do you return as the result code?

* You can't be sure that the caller will remember to check the status
code. In fact, you can be sure that the caller WILL forget sometimes!
(This is human nature.) This leads to the frequent problem that by the
time a caller checks the status code, the original error has been lost
and the program is working with garbage.

* Even if you remember to check the status code, it complicates the code,
makes it less readable, confuses the intent of the code, and often leads
to the Arrow Anti-pattern: http://c2.com/cgi/wiki?ArrowAntiPattern

That last argument is critical. Exceptions exist to make writing correct
code easier to write, understand and maintain.

Python uses special result codes in at least two places:

str.find(s) returns -1 if s is not in the string
re.match() returns None is the regular expression fails

Both of these are error-prone. Consider a naive way of getting the
fractional part of a float string:

>>> s = "234.567"
>>> print s[s.find('.')+1:]

567

But see:

>>> s = "234"
>>> print s[s.find('.')+1:]

234

You need something like:

p = s.find('.')
if p == -1:
print ''
else:
print s[p+1:]



Similarly, we cannot safely do this in Python:


>>> re.match(r'\d+', '123abcd').group()

'123'
>>> re.match(r'\d+', 'abcd').group()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

You need to do this:

mo = re.match(r'\d+', '123abcd')
if mo is not None: # or just `if mo` will work
mo.group()


Exceptions are about making it easier to have correct code. They're also
about making it easier to have readable code. Which is easier to read,
easier to understand and easier to debug?

x = function(1, 2, 3)
if x != -1:
y = function(x, 1, 2)
if y != -1:
z = function(y, x, 1)
if z != -1:
print "result is", z
else:
print "an error occurred"
else:
print "an error occurred"
else:
print "an error occurred"


versus:


try:
x = function(1, 2, 3)
y = function(x, 1, 2)
print "result is", function(y, x, 1)
except ValueError:
print "an error occurred"



In Python, setting up the try...except block is very fast, about as fast
as a plain "pass" statement, but actually catching the exception is quite
slow. So let's compare string.find (which returns an error result) and
string.index (which raises an exception):


>>> from timeit import Timer
>>> setup = "source = 'abcd'*100 + 'e'"
>>> min(Timer("p = source.index('e')", setup).repeat())

1.1308379173278809
>>> min(Timer("p = source.find('e')", setup).repeat())

1.2237567901611328

There's hardly any difference at all, and in fact index is slightly
faster. But what about if there's an exceptional case?


>>> min(Timer("""

.... try:
.... p = source.index('z')
.... except ValueError:
.... pass
.... """, setup).repeat())
3.5699808597564697
>>> min(Timer("""

.... p = source.find('z')
.... if p == -1:
.... pass
.... """, setup).repeat())
1.7874350070953369


So in Python, catching the exception is slower, in this case about twice
as slow. But remember that the "if p == -1" test is not free. It might be
cheap, but it does take time. If you call find() enough times, and every
single time you then test the result returned, that extra cost may be
more expensive than catching a rare exception.

The general rule in Python is:

* if the exceptional event is rare (say, on average, less than about one
time in ten) then use a try...except and catch the exception;

* but if it is very common (more than one time in ten) then it is faster
to do a test.


> My observation is contradicted to the above statement by Henning. If my
> observation is wrong, please just ignore my question below.
>
> 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?


Yes.

Python's aim is to be fast *enough*, without necessarily being as fast as
possible.

Python aims to be readable, and to be easy to write correct, bug-free
code.


--
Steven

Aahz 01-01-2010 08:43 AM

Re: Exception as the primary error handling mechanism?
 
In article <mailman.300.1262323578.28905.python-list@python.org>,
Benjamin Kaplan <benjamin.kaplan@case.edu> wrote:
>
>In Python, throwing exceptions for expected outcomes is considered
>very bad form [...]


Who says that? I certainly don't.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.

Martin v. Loewis 01-01-2010 10:34 AM

Re: Exception as the primary error handling mechanism?
 
Peng Yu wrote:
> I observe that python library primarily use exception for error
> handling rather than use error code.

[...]
> It says "Another popular design flaw—namely, throwing exceptions for
> expected outcomes—also causes inefficiencies because catching and
> handling exceptions is almost always slower than testing a return
> value."
>
> My observation is contradicted to the above statement by Henning. If
> my observation is wrong, please just ignore my question below.


Your observation is not wrong, but, as Benjamin already explained,
you are misinterpreting Michi Henning's statement. He doesn't condemn
exception handling per se, but only for the handling of *expected*
outcomes. He would consider using exceptions fine for *exceptional*
output, and that is exactly the way they are used in the Python API.

Notice that in cases where the failure may be expected, Python
also offers variants that avoid the exception:
- if you look into a dictionary, expecting that a key may not
be there, a regular access, d[k], may give a KeyError. However,
alternatively, you can use d.get(k, default) which raises no
exception, and you can test "k in d" in advance.
- if you open a file, not knowing whether it will be there,
you get an IOError. However, you can use os.path.exists in
advance to determine whether the file is present (and create
it if it's not).

So, in these cases, it is a choice of the user to determine whether
the error case is exceptional or not.

Regards,
Martin

Jonathan Gardner 01-01-2010 10:43 AM

Re: Exception as the primary error handling mechanism?
 
On Jan 1, 12:43*am, a...@pythoncraft.com (Aahz) wrote:
> In article <mailman.300.1262323578.28905.python-l...@python.org>,
> Benjamin Kaplan *<benjamin.kap...@case.edu> wrote:
> >In Python, throwing exceptions for expected outcomes is considered
> >very bad form [...]

>
> Who says that? *I certainly don't.


Agreed.

int("asdf") is supposed to return what, exactly? Any language that
tries to return an int is horribly broken.

Andreas Waldenburger 01-01-2010 02:01 PM

Re: Exception as the primary error handling mechanism?
 
On Fri, 01 Jan 2010 11:34:19 +0100 "Martin v. Loewis"
<martin@v.loewis.de> wrote:

> Your observation is not wrong, but, as Benjamin already explained,
> you are misinterpreting Michi Henning's statement. He doesn't condemn
> exception handling per se, but only for the handling of *expected*
> outcomes. He would consider using exceptions fine for *exceptional*
> output, and that is exactly the way they are used in the Python API.


May I point out at this point that "exceptional" does not mean
"unexpected"? You catch exceptions, not unexpectations. An exception
is rare, but not surprising. Case in point: StopIteration.

To put it differently: When you write "catch DeadParrot", you certainly
expect to get a DeadParrot once in a while -- why else would you get it
in your head to try and catch it? An unexpected exception is the one
that crashes your program.

/W

--
INVALID? DE!


Steven D'Aprano 01-01-2010 02:24 PM

Re: Exception as the primary error handling mechanism?
 
On Fri, 01 Jan 2010 02:43:21 -0800, Jonathan Gardner wrote:

> On Jan 1, 12:43Â*am, a...@pythoncraft.com (Aahz) wrote:
>> In article <mailman.300.1262323578.28905.python-l...@python.org>,
>> Benjamin Kaplan Â*<benjamin.kap...@case.edu> wrote:
>> >In Python, throwing exceptions for expected outcomes is considered
>> >very bad form [...]

>>
>> Who says that? Â*I certainly don't.

>
> Agreed.
>
> int("asdf") is supposed to return what, exactly? Any language that tries
> to return an int is horribly broken.



[sarcasm]
No no, the right way to deal with that is have int("asdf") return some
arbitrary bit pattern, and expect the user to check a global variable to
see whether the function returned a valid result or not. That's much
better than catching an exception!
[/sarcasm]


--
Steven

Steven D'Aprano 01-01-2010 02:49 PM

Re: Exception as the primary error handling mechanism?
 
On Fri, 01 Jan 2010 00:26:09 -0500, Benjamin Kaplan wrote:

> On Thu, Dec 31, 2009 at 11:47 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
>> I observe that python library primarily use exception for error
>> handling rather than use error code.
>>
>> In the article API Design Matters by Michi Henning
>>
>> Communications of the ACM
>> Vol. 52 No. 5, Pages 46-56
>> 10.1145/1506409.1506424
>> http://cacm.acm.org/magazines/2009/5...tters/fulltext
>>
>> It says "Another popular design flaw—namely, throwing exceptions for
>> expected outcomes—also causes inefficiencies because catching and
>> handling exceptions is almost always slower than testing a return
>> value."
>>
>> My observation is contradicted to the above statement by Henning. If my
>> observation is wrong, please just ignore my question below.
>>
>> 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?
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>

> Read the quote again "Another popular design flaw—namely, throwing
> exceptions *for expected outcomes*"
> In Python, throwing exceptions for expected outcomes is considered very
> bad form (well, except for StopIteration but that should almost never be
> handled directly by the programmer).



Exceptions are *exceptional*, not "errors" or "unexpected". They are
exceptional because they aren't the "normal" case, but that doesn't mean
they are surprising or unexpected. Are you surprised that your "for x in
range(1000)" loop comes to an end? Of course you are not -- it is
completely expected, even though less than 1% of the iterations are the
last loop. The end of the sequence is EXCEPTIONAL but not UNEXPECTED.

If you program without expecting that keys can sometimes be missing from
dictionaries (KeyError), or that you might sometimes have to deal with a
list index that is out of range (IndexError), or that the denominator in
a division might be zero (ZeroDivisionError), then you must be writing
really buggy code. None of these things are unexpected, but they are all
exceptional.

The urllib2 module defines a HTTPError class, which does double-duty as
both an exception and a valid HTTP response. If you're doing any HTTP
programming, you better expect to deal with HTTP 301, 302 etc. codes, or
at least trust that the library you use will transparently handle them
for you.


> To answer why people recommend using "Easier to Ask Forgiveness than
> Permission" as opposed to "Look Before You Leap" : Because of the way
> it's implemented, Python works quite differently from most languages. An
> attribute look-up is rather expensive because it's a hash table look-up
> at run time. Wrapping a small piece of code in a try block however,
> isn't (relatively) very expensive at all in Python.


It's not just relatively inexpensive, it's absolutely inexpensive: it
costs about as much as a pass statement in CPython, which is pretty much
as cheap as it gets. (If anyone can demonstrate a cheaper operation
available from pure Python, I'd love to see it.)


> It's only catching the exception that's expensive,


True.


> but if you're catching the exception,
> something has gone wrong anyway and performance isn't your biggest
> issue.



The second try...except clause in the urllib2 module reads:

try:
kind = int(kind)
except ValueError:
pass

In this case, the error is simply ignored. Nothing has gone wrong.


Here's an example from my own code: I have an API where you pass a
mapping (either a dict or a list of (key, value) tuples) to a function.
So I do this:

try:
it = obj.iteritems()
except AttributeError:
it = obj
for key, value in it:
do_stuff()



There's nothing wrong with catching exceptions.


--
Steven


All times are GMT. The time now is 07:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.