Simple unicode-safe version of str(exception)?

Jim jim.hefferon at gmail.com
Tue Apr 29 20:50:49 EDT 2008


I often struggle with the problem outlined in part in this thread.  I
know that I'm going to repeat some of what is said elsewhere but I'd
like to present the question all in one place.

I believe that the routines in the Python standard library do not
document which exceptions they could raise (I understand some reasons
why, but they nontheless do not).  So I often find out the hard way
that a library call can raise an exception that I was not smart enough
to anticipate.  So I often find myself doing
  try:
     make_a_routine_call(x,y,z)
  except AnticipatedError, err:
     do_something_with_it(x,y,z,err)
  exception Exception, err:
     print "something crazy happened "+str(err)
where the print statement is just supposed to give me some idea of
what happened.  (I actually usually log it instead of printing it.  If
there is a better way, I'd love to hear it.)  But "str(err)" sometimes
fails, as the OP noted.

Further, xtr(err) alone is not enough, I believe.  I believe that some
routines in the standard library do not return strings (by that I mean
something approximately like "err.value is a pair"; forgive me, I
don't remember the full details).

Surely as lovely a language as Python has a better idiom for dealing
with exceptions in the standard library than something that works out
to
  print "something crazy happened"+" ".join([xtra(x) for x in err])
?  (I would understand better if it was a user's code; they are free
to do what works for them, of course.)  But I've been unable to think
of one and I haven't seen in this thread another direction.  Is there
one?

Jim



More information about the Python-list mailing list