Help with trapping an exception

P

pythos

I have a piece of code like this:

try:
some code
except:
print >> sys.stderr, "error: ", sys.exc_info()


When an exception is thrown from the code, what I see on the console is this:

error: (<class exceptions.NameError at 0x01E7A378>, <exceptions.NameError
instance at 0x09752C50>, <traceback object at 0x096A1408>)

I had a lot of trouble figuring out what was causing the NameError. I finally
figured it out by removing the "try" and "except" statements so that my code
didn't catch any exceptions. When I did that, I saw the following on my
console:

Traceback (most recent call last):
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 79, in Do
os.path.walk(baseDirectory, ProcessDirectory, Environment)
File "c:\Program Files\xxxxxx\Python Libraries\lib\ntpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, 'FileOpen', {
NameError: global name 'Environment' is not defined


Now that was much more helpful. Once I knew that 'Environment' was not
defined, I easily figured out the problem. But I need to use "try" and
"except" to catch the exception, otherwise my program will end abruptly
without finishing. So how can I see the "NameError: global name 'Environment'
is not defined" message in the "except" section of my code? A call to
sys.exc_info() doesn't show it. Is there another function I can use? Thanks.
 
P

Phil Frost

Take a look at the traceback standard module to format exceptions
nicely. Another thing you can do is this:

try:
{}['fu']
except Exception, x:
print x

That asigns the exception to x in the except block. All exceptions can
be converted to strings which are usually short descriptions of the
problem. One problem is KeyError, which converts to just the bad key,
for example the above just prints "fu" which is less than helpful.
Another catch of the above is that it will only catch subclasses of
Exception, which is almost everything except a few like StopIteration or
user classes that don't subclass Exception.
 
R

Roger Binns

pythos said:
not defined, I easily figured out the problem. But I need to use
"try" and "except" to catch the exception, otherwise my program will
end abruptly without finishing. So how can I see the "NameError:
global name 'Environment' is not defined" message in the "except"
section of my code? A call to sys.exc_info() doesn't show it. Is
there another function I can use? Thanks.

I suggest using this code from the Python cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215

I also strongly recommend getting the dead tree version as
well. That recipe and many others are in it, and it is all
well laid out with lots of good discussion.

Roger
 
P

pythos

Take a look at the traceback standard module to format exceptions
nicely.

Ah, this looks like what I want. Although when I use either
traceback.print_tb(sys.exc_info()[2]) or traceback.print_exc(), it only shows
the most recent call in the traceback. This is what it shows:

Traceback (most recent call last):
File "C:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, 'FileOpen', {


Why doesn't it show everything, which would be:

Traceback (most recent call last):
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 79, in Do
os.path.walk(baseDirectory, ProcessDirectory, Environment)
File "c:\Program Files\xxxxxx\Python Libraries\lib\ntpath.py", line 318, in
walk
func(arg, top, names)
File "c:\Documents and Settings\xxxxx\rot.PspScript", line 36, in
ProcessDirectory
App.Do(Environment, 'FileOpen', {
NameError: global name 'Environment' is not defined
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top