Fixed exception handling for py3