[Tutor] Exception Handling and Stack traces
Michael Powe
michael at trollope.org
Fri Sep 10 20:47:13 CEST 2010
On Fri, Sep 10, 2010 at 04:42:35PM +0200, Peter Otten wrote:
> Michael Powe wrote:
> > 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.
> >> 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
> > 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).
> Sorry, the as-clause is *not* necessary. The relevant difference between the
> correct and the wrong approach is that you must not instantiate the
> exception:
> 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:
> ... print "caught"
> ...
> caught
>
> I just put in the as-clause to show an easy way to print the exception. I
> did not anticipate that it would obscure the message.
Hello,
No problem, I am working on getting this sorted out. The
documentation seems to be written as reminder for people who already
know how this stuff works, rather than as a clear explanation for
anybody working with it.
Eventually, I arrived at a workable conclusion by wrapping only the
call in main and using your suggested 'as' clause. This successfully
suppresses the traceback and gives a useable error message. Although,
in the case of URLError, 'getaddrinfo failed' may not actually mean
much to the end user, it'll have to do.
I don't like the fact that I cannot locate my thrown exception at the
point of throwing -- i.e., I don't necessarily mind catching the
exception in main but I would like to be able to print out exactly
where the exception occurred. This is more useful when
troubleshooting. However, an entire stacktrace is unacceptably
verbose.
Thanks.
mp
--
Michael Powe michael at trollope.org Naugatuck CT USA
It's easier to fight for one's principles than to live up to them.
-------------- 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/a98cc8e4/attachment.pgp>
More information about the Tutor
mailing list