Cleaned up invalid python code

Changed throw(exception) to raise Exception(), and added logging to the
error.