Fix exception handling for python3