On Fri, Sep 10, 2010 at 03:56:51PM +0200, Peter Otten wrote:
> Michael Powe wrote:
> 
> > I can't work out how to suppress stacktrace printing when exceptions
> > are thrown.
> 
> [snip rant]
> 
> It might have been a good idea to read a tutorial like
> 
> http://docs.python.org/tutorial/errors.html#handling-exceptions
 
> or ask before you got annoyed enough to write that rant ;)

Hello,

Thanks for the reply.  Stupid me, I just read a half dozen articles on
the web about python exception handling, including some at
docs.python.  At no point is the 'as' clause discussed as being
required.

Note that in section 8.3 of that article, the statement is made that
if the exception matches the the exception type in the following
format, the statements within the except clause are executed.

except URLError :
           # do something

That in fact, seems to me to be incorrect.  It is not my experience
(e.g., print statements are not executed in the example I gave and the
sys.exit() is not called).

I'll follow up on your suggestions.  I appreciate the help.

Thanks.

mp
 
> To catch an exception you have to put the class into the except clause, not 
> an instance. Basic example, using 2.6 syntax:
> 
> WRONG:
> 
> >>> try:
> ...     1/0
> ... except ZeroDivisionError("whatever"):
> ...     print "caught"
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 2, in <module>
> ZeroDivisionError: integer division or modulo by zero
> 
> CORRECT:
> 
> >>> try:
> ...     1/0
> ... except ZeroDivisionError as e:
> ...     print "caught", e
> ...
> caught integer division or modulo by zero
> 
> Peter
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- 
Michael Powe            mich...@trollope.org            Naugatuck CT USA
"...we built a new continent in cyberspace where we could go live and be
free. And the nice thing is that, because we built it, we didn't have
to steal it from aboriginal peoples. It was completely empty, and we
invite everyone to live there with us. No immigration restrictions.
There's room for everyone in the world. Come live in the free world
and be free. That's our idea."  -- Richard Stallman

Attachment: pgpnp3PNLKdQk.pgp
Description: PGP signature

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to