<br>On Fri, Jan 1, 2010 at 5:36 PM, Peng Yu <span dir="ltr"><<a href="mailto:pengyu.ut@gmail.com">pengyu.ut@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

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

<div><br></div><div>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. </div>

<div><br></div><div>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)</div>

<div><br></div><div>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.</div>

<div><br></div></div><div name="mailplane_signature">--S</div>