How to catch an index error
<p>Given <code>mylist = [0, 1]</code></p>

<pre><code>def catch_index_error(value):
    try:
        return value
    except IndexError:
        return None

catch_index_error(mylist[5])
</code></pre>

<p>returns an <code>IndexError</code></p>

<p>The argument is evaluated prior to the function being executed and therefore the function can't catch the exception. Is there a way of catching it?</p>

<pre><code>try:
    catch_index_error(mylist[5])
except IndexError:
    do something
</code></pre>

<p>The error occurs in the call</p>

python