[Tutor] error trap

Daniel Coughlin kauphlyn@speakeasy.org
Fri, 18 Jan 2002 02:34:46 -0800 (PST)


On Fri, 18 Jan 2002, kirk Bailey wrote:

> 
> 
> Danny Yoo wrote:
> > 
> > On Fri, 18 Jan 2002, kirk Bailey wrote:
> > 
> > > ok, built a pretty blunt weapon to handle errors.
> > >
> > > IN TLpost.py;
> > > for to_addr in members :                # for each address in
> > > 'listnamemembers',
> > >         # print "to_addr: ", to_addr
> > >         # print "from_addr: ", from_addr
> > >         # print "msg=" + CRLF, msg
> > >         try:
> > >                 server.sendmail(from_addr, to_addr, msg)        # 400 send envlope and msg!
> > >         except:
> > >                 print "Bad address='"+to_addr+"'"
> > 
> > It might be good to also print out the error message that the system
> > itself gives.  Every exception that gets thrown brings with it a useful
> > error message for debugging purposes.  So:
> > 
> > ###
> > try:
> >     server.sendmail(from_addr, to_addr, msg)
> > except Exception, e:
>          ^^^^^^^^^  ^ ??? more data please?
> >     print "Bad address='"+to_addr+"'"
> >     print "exception message:", e <-- this must be ther actual error reported?
>           
> > ###
> > 
> > is more informative, if a bit uglier.  Still, it'll help if anything weird
> > happens.
> 
> Should I install a wierdness magnet? Maybe a prayer wheel to obtain
> blessings?
> 
> To perdition with cosmetics, just help me understand it!
> (we can pretty up the listing later...)
> 
> > > Testing with a known bad address in the list, it comes up with an
> > > error print, and finishes the job.
> > 
> > The reason why we still want to print out the exception is as a worst-case
> > thing: what if there were some other way to make server.sendmail() die
> > other than a bad 'to_addr'?  There probably isn't a way, but let's make
> > certain that, in the case that we've overlooked something, we'll still get
> > useful information from the error.
> 
> Sounds like a plan to me, just tell me a little more bout this
> statement I threw carrots at.
> (bad pun I know.)

The statement you threw carrots at:

1. Exception is the base class for all exceptions. All Exceptions are built from 
this class.

2. The variable e will be an instance of the Exception class. Adding this 
variable is optional to except statement.

3. Depending on the exception, you may get more information about it by 
reviewing 
e.args. 

4. You can refine you exception by saying 
>>> except SpecificException, s
such as IndexError or TypeError or you can define your own by extending the 
Exception class.

5. And last! you can play in the interpreter like this:
>>> try:
>>>	l = [1]
>>>	l[2]
>>>except Exception, e:
>>>	print e
list out of range
>>> l = [1]
>>> l[2]
>>>
Traceback (most recent call last):
...
IndexError: list out of range

Note that IndexError is a specific type of exception, derived from the Exception 
class.

and if this were one of the problems you might encounter in you code
you coulde write 
>>> except IndexError, i:
	whatever

in this way you can specify what way you want to handle specific exceptions.


hope this helps!

Daniel