Get the index that caused an IndexError exception

python, python-2.7

Solution

Only manually; for example:

arr = [1,2,3]
try:
    try_index = 42
    print(arr[try_index])
except IndexError:
    print 'Index', try_index, 'caused an IndexError'

Problem

Is it possible to get the index that caused an `IndexError` exception? Sample code : ``` arr = [0, 2, 3, 4, 5, 6, 6] try: print arr[10] # This will cause IndexError except IndexError as e: print e.args # Can I get which index (in this case 10) caused the exception? ```

Original source