better exception handling for python 3