[Tutor] error trap

Andy W toodles@yifan.net
Sat, 19 Jan 2002 02:49:58 +0800


Hiya

> OK, I am trying to handle it with a default except statement, and
> capture the error feedback, but I get nothing but complaints when I
> try THIS:

It should be "except Exception, e:", not "except e:".
Try that out.

Andy

>
> 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 e:
>                 print "POSSIBLE Bad address='"+to_addr+"' ?"
>                 print "exception message:", e
>
> So I removed the 'e' in the line 'except e:' and the second print
> statement in the exception block.
> This handles bad emails, so preserves function and continues delivery.
> Still, I would love to see that error feedback if possible- maybe have
> it email me with it.
>
> Daniel Coughlin wrote:
> >
> > 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
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> --
>
>
>  -Respectfully,
>               -Kirk D Bailey
>                Consulting Loose Cannon
>
> end
>
>
>
>   www.howlermonkey.net                 highprimate@howlermonkey.net
>   www.sacredelectron.org                         idiot1@netzero.net
>   www.tinylist.org                              grumpy@tinylist.org
> ----------------------------------------------------
> Sign Up for NetZero Platinum Today
> Only $9.95 per month!
> http://my.netzero.net/s/signup?r=platinum&refcd=PT97
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>