Fix exception catching to be compatible with Python 3.