[Newbie] error handling

Cameron Laird claird at lairds.us
Wed Oct 11 14:39:59 EDT 2006


In article <mailman.325.1160583556.11739.python-list at python.org>,
Fulvio  <fulvio at tm.net.my> wrote:
>***********************
>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.



More information about the Python-list mailing list