[Tutor] Exception Handling and Stack traces

Michael Powe michael at trollope.org
Fri Sep 10 16:28:36 CEST 2010


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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- 
Michael Powe		michael at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20100910/a8331a0f/attachment.pgp>


More information about the Tutor mailing list