<story><title>Python Idioms [pdf]</title><url>http://safehammad.com/downloads/python-idioms-2014-01-16.pdf</url></story><parent_chain><item><author>csense</author><text>I disagree with promoting try &amp;#x2F; catch.  Exceptions like ValueError can really happen almost anywhere, so it is usually better to sanitize your inputs.&lt;p&gt;E.g. something like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    try: 
        something = myfunc(d[&amp;#x27;x&amp;#x27;])
    except ValueError:
        something = None
&lt;/code&gt;&lt;/pre&gt;
The programmer&amp;#x27;s intent is probably to only catch errors in the key lookup d[&amp;#x27;x&amp;#x27;], but if there is some bug in the implementation of myfunc() or any of the functions called by myfunc() which causes a ValueError to unintentionally be raised, it will be caught by the except.&lt;p&gt;For dictionary lookups specifically, get() is usually preferable:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    something = d.get(&amp;#x27;x&amp;#x27;)
    if something is not None:
        something = myfunc(something)
&lt;/code&gt;&lt;/pre&gt;
Or if dictionary may potentially contain None values:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    if &amp;#x27;x&amp;#x27; in d:
        something = myfunc(d[&amp;#x27;x&amp;#x27;])&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>smnrchrds</author><text>Not to counter your point, but I would like to quote from PEP8 here:&lt;p&gt;Additionally, for all try&amp;#x2F;except clauses, limit the try clause to the absolute minimum amount of code necessary. Again, this avoids masking bugs.&lt;p&gt;Yes:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    try:
        value = collection[key]
    except KeyError:
        return key_not_found(key)
    else:
        return handle_value(value)
&lt;/code&gt;&lt;/pre&gt;
No:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    try:
        # Too broad!
        return handle_value(collection[key])
    except KeyError:
        # Will also catch KeyError raised by handle_value()
        return key_not_found(key)&lt;/code&gt;&lt;/pre&gt;</text></comment>
<story><title>Python Idioms [pdf]</title><url>http://safehammad.com/downloads/python-idioms-2014-01-16.pdf</url></story><parent_chain><item><author>csense</author><text>I disagree with promoting try &amp;#x2F; catch.  Exceptions like ValueError can really happen almost anywhere, so it is usually better to sanitize your inputs.&lt;p&gt;E.g. something like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    try: 
        something = myfunc(d[&amp;#x27;x&amp;#x27;])
    except ValueError:
        something = None
&lt;/code&gt;&lt;/pre&gt;
The programmer&amp;#x27;s intent is probably to only catch errors in the key lookup d[&amp;#x27;x&amp;#x27;], but if there is some bug in the implementation of myfunc() or any of the functions called by myfunc() which causes a ValueError to unintentionally be raised, it will be caught by the except.&lt;p&gt;For dictionary lookups specifically, get() is usually preferable:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    something = d.get(&amp;#x27;x&amp;#x27;)
    if something is not None:
        something = myfunc(something)
&lt;/code&gt;&lt;/pre&gt;
Or if dictionary may potentially contain None values:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    if &amp;#x27;x&amp;#x27; in d:
        something = myfunc(d[&amp;#x27;x&amp;#x27;])&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>middleclick</author><text>While not disagreeing with your case, there are some cases where I try to follow this rule because in most of those cases, I would expect that there is no exception and if there is, then catch it.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  try:
      open(FILE)
  except IOError:
      it failed&lt;/code&gt;&lt;/pre&gt;</text></comment>