[Tutor] Exception Handling and Stack traces
michael at trollope.org
Fri Sep 10 15:09:00 CEST 2010
I can't work out how to suppress stacktrace printing when exceptions
I want the thrown exception to pass a message on the console, just
like Java does when I catch an exception and print e.getMessage().
I tried some of the examples of controlling traceback through the
I have to admit, this is one of the cases where I wonder why it has to
be made so bloody complicated. The other day, I read an article by
Guido in which he was proposing to remove 'map' and 'filter' from the
language because he though the developer shouldn't have to think too
much about using them. But the exception handling is so non-intuitive
that the time saved from not having to think about using 'map' is
spent instead on trying to figure out how to promote a message to the
commandline when a method fails.
It seems that exceptions automatically are thrown up, so even if I put
a 'try/except' clause in a method; when that method fails, the
exception will still be thrown up to the top of the call chain. So,
there seems to be no reason whatever to catch an exception in any
method except the top caller (i.e., 'main').
def getData(auth) :
# create opener with auth headers here
data = opener.open(url)
except urllib2.URLError("Badly formed URL") :
formatted_lines = traceback.format_exc().splitlines()
This method is called by another method, printOutput(), which
processes the return data (XML from web service).
That method is called in main:
[printOutput(w) for w in weeks]
All I want to see is that if the exception is thrown in getData(), the
message is printed to stdout and the script exits. Instead, I get the
stacktrace printed back down from main, I don't get the exception
handled in getData (i.e., the error message and exit).
Now, I'm sure somebody is going to explain to me why it's so
unreasonable to think that I ought to be able to get an error message
from e.getMessage() and a stacktrace from e.printStacktrace() and that
I ought to be able to choose between the two. Because, it seems that
python is determined that I should have a stacktrace whether I want
one or not.
Sorry, I'm more than a little annoyed because even the example of
using the traceback module from the python docs does not provide the
handling that it is supposed to; and I really think this level of
complexity for handling exceptions cleanly is just unwarranted. I
need to be able to give this script to someone who will want to be
able to read the error output without having to be a Python programmer
experienced in reading stack traces. e.g. a "Badly formed URL"
message that tells them they set up the parameters for connecting to
the web service incorrectly.
Hopefully, you can get past the rant and help me solve this problem.
Michael Powe michael at trollope.org Naugatuck CT USA
"The secret to strong security: less reliance on secrets."
-- Whitfield Diffie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 197 bytes
Desc: not available
More information about the Tutor