osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How do I find what kind of exception is thrown.


On 05Sep2017 10:50, Antoon Pardon <antoon.pardon at vub.be> wrote:
>Python 2.6.4 on a solaris box.
>
>I have a program in which all kind of excptions can be thrown and caugth.
>The main program is something like below:
>
>try:
>    do_stuff
>except Exception:
>    log unexpected trouble
>
>Now I found the following in the logs: [Errno 131] Connection reset by peer
>
>This is a problem I would like to catch earlier however I have no idea what
>exception I would have to catch in order to treat this case.

Almost anything which says "Errno ..." is almost always an OSError (or an 
IOError as I found the other day). I try to be quite picky about these. So for 
example:

  try:
    fp = open(filename)
  except OSError as e:
    if e.errno == errno.ENOENT:
      # file missing
      ... act as if the file were empty ...
    else:
      # other badness - let the exception escape
      raise

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)