Clean up exception handling in tests to work with Python 3.