[Tutor] Exception Handling and Stack traces

Evert Rol evert.rol at gmail.com
Fri Sep 10 15:50:57 CEST 2010

> I can't work out how to suppress stacktrace printing when exceptions
> are thrown.
> 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
> traceback module.
> 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').  
> Example:
> def getData(auth) :
> 	# create opener with auth headers here
> 	try :
> 		data = opener.open(url)
> 	except urllib2.URLError("Badly formed URL") :
> 		formatted_lines = traceback.format_exc().splitlines()
> 		print formatted_lines[0]
> 		print formatted_lines[-1]
> 		sys.exit(1)
> 	return data

This is a bit of a guess, but as far as I know, you can catch exceptions like that. 

    data = opener.open(url)
except urllib2.URLError as msg:
    print msg

If you're using an older version of Python, you'll need:

    data = opener.open(url)
except urllib2.URLError, msg:
    print msg

In your example, you are *creating* an exception (but doing nothing with it; I have no idea what happens if you have a line like "except <Exception instance>". Perhaps it tries to compare one on one with that instance, but if it compares by id, that will not work).
In this way, you're not catching the exception. So, it will be pass your except clause, and just do what it always does: print the whole exception's traceback. Which is probably what you're seeing.


> 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.
> Thanks.
> mp
> -- 
> Michael Powe		michael at trollope.org		Naugatuck CT USA
> "The secret to strong security: less reliance on secrets."
> -- Whitfield Diffie
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list