Replace Python 2.5-style exception syntax with 2.6-style

Also fixes up an exception handling block which captured an exception
under one name and then tried to use the object under a different name.