Exception as the primary error handling mechanism?

Lie Ryan lie.1296 at gmail.com
Fri Jan 1 16:24:41 CET 2010


On 1/1/2010 3:47 PM, 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/24646-api-design-matters/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?

Simple, when an exception is thrown and I don't catch it, the exception 
terminates the program immediately and I got a traceback showing the 
point of failure. When I return error value and I don't check for it, I 
passed passed errors silently and gets a traceback forty-two lines later 
when trying to use the resources I failed to acquire forty-two lines prior.

 > Is it because the efficiency
 > is not the primary goal of python?

Efficiency is not primary goal of python, but since python encourages 
EAFP (Easier to Ask Forgiveness than Permission); the design decisions 
chosen makes setting up a try-block much cheaper than a language 
designed over LBYL (Look Before You Leap) and return codes.



More information about the Python-list mailing list