better exception handling to support python v2.7+