[Newbie] error handling

F

Fulvio

***********************
Your mail has been scanned by InterScan MSS.
***********************


Hello there,

Simple question : how do I manage errors by the use "try/except" clause.
Example:
If I'd like to catch error coming from a function call that's using IMAP4
class, which error may raise such class?
In other words I've doubts about which error object I should put after
the "except" statement in order to trap the wanted error.

Is there, somehow, the way to list the errors from a class or function, prior
digging into the source?

F
 
B

Bernard

Hi Fulvio,

I often use this try except to find out about what type of errors might
happen in my code:
I use it when I really don't know what might happen.
try:
# do something
except:
print "Unexpected error:", sys.exc_info()[0]
continue

once it catches an error, just take a good look at the error message
and you'll be able to extend the try except like this:

try:
# do something
except KeyError:
print "this is a KeyError"
continue
except TypeError:
print "this is a TypeError"
continue
except:
print "Unexpected error:", sys.exc_info()[0]
os.system("pause")
continue
 
C

Cameron Laird

***********************
Your mail has been scanned by InterScan MSS.
***********************


Hello there,

Simple question : how do I manage errors by the use "try/except" clause.
Example:
If I'd like to catch error coming from a function call that's using IMAP4
class, which error may raise such class?
In other words I've doubts about which error object I should put after
the "except" statement in order to trap the wanted error.

Is there, somehow, the way to list the errors from a class or function, prior
digging into the source?

F

No.

It's an important question. No, there is NOT in general a way
to interrogate a class or function to determine the range of
exceptions it might throw.

Working programmers take a couple of approaches to analyze
exceptions:
A. Read documentation. Good API documentation
must specify exceptions thrown.

It's very, *very* rare for Python extensions
to document their exceptions accurately.
B. Incrementally refine your source. I might
write

try:
do_imap_stuff()
except:
raise

for my first round of coding. Then I exercise
a couple of use cases, and reach

try:
do_imap_stuff()
except IOError:
handle_this_exception()
except ZeroDivisionError:
handle_that_exception()
except:
raise

and so on.

Exceptions are hard to get right. I keep threatening to
write a book on the subject, mostly so I can learn what
the correct answers are.
 
G

Gabriel Genellina

I just found this webpage showing the most common exceptions:
http://pydoc.org/1.5.2/exceptions.html

Why not refer to the *current* documentation?
http://docs.python.org/lib/module-exceptions.html
(You already have it installed with Python)

1.5.2 is way too old!


--
Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
F

Fulvio

|Why not refer to the *current* documentation?

Gabriel,

Thank you, some time I (we) forget to look at the most obvious place ;)
Meanwhile yesterday I found some useful info on the web, which an *except*
statement needs a string object or class for a named exception.
Now I got the meaning and " sys.exc_info()[0]" Bernard's idea is useful to
fish out more clues.

F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top