Updated exception handling to be compatible with Python 2.x and 3.x

This change makes the exception handling compatible with Python 2.x and Python 3.x. More testing would be needed to ensure the project as a whole works in Python 3.x but at least the module can be imported in Python 3.x.