Processing files is more complex. In this example we open the file, read the first line which is a number and use that for division. Running this code will stop in the middle with a ZeroDivisionError exception. Even though we wrapped the code in a try: statement, this only caught the excepting we listed there.

#!/usr/bin/env python
from __future__ import print_function

# 3 of the 4 file exist
files = 'one.txt', 'zero.txt', 'two.txt', 'three.txt'

for f in files:
  print("before " + f)
    fh = open(f, 'r')
    number = int(fh.readline())
    print(30 / number)
  except IOError:
    print("  Could not open " + f)
  print("after  " + f)

# before one.txt
# 30
# after  one.txt
# before zero.txt
# Traceback (most recent call last):
#   File "", line 11, in <module>
#     print(30 / number)
# ZeroDivisionError: integer division or modulo by zero

except Exception:

except Exception as e:

What can be done?

You can list more than one exceptions to be caught: except (IOError, ZeroDivisionError): Of course in this case you might want to change the text of the error you print. If you'd like to have more details about the exception you can add "as e" and get the exception object in the variable e. except (IOError, ZeroDivisionError) as e: print e Alternatively you can group exception in separate except statements except IOError: print " Could not open " + f except ZeroDivisionError: print " Error zero in " + f In case you don't know what other exception might be there, you could even catch all exceptions except Exception as e: print e