468,733 Members | 1,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Catching error text like that shown in console?

I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott
Dec 9 '05 #1
3 1618

Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott


I don't have an exact answer for you, but since no one else replied:

look at subprocess module, and os.popen4()

http://docs.python.org/lib/os-newstr...#os-newstreams
http://docs.python.org/lib/module-subprocess.html
http://mail.python.org/pipermail/pyt...er/305671.html

Hint: mentioning your O/S is pretty essential; for many issues, what
version of py and how you installed it also

Dec 9 '05 #2

Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback

Anyone know how I can do this or if it's possible? Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

-Pete Schott


1. Create a function to be run when an exception happens.

def excepthook(self, type, value, tb):
#
# This function allows the user to redefine what happens if the program
# aborts due to an uncaught exception.
import traceback
#
# Get traceback lines and append the current session log
#
tblines=traceback.format_exception(type, value, tb)
#
# Print these lines somewhere. Normally you would log to a file
#
print "traceback exception encountered"
print "--------------------Begin traceback lines-------------------------"
print tblines
print "--------------------End traceback lines---------------------------")
map(sys.stdout.writelines, tblines) # Always write exceptions to screen
sys.exit(2)

2. Tell python you want to call this function when exception occurs.

sys.excepthook=exceptHandler
Dec 10 '05 #3
Peter A. Schott wrote:
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later. I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work. I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
1. something failed
2. here's the error output/traceback


If you just want to catch exceptions and print a traceback, use the
traceback module. This is handy for example if you are processing a
number of items and don't want a failure in one to abort the whole loop:

import traceback
for work in thingsToDo:
try:
doSomeWork(work)
except:
traceback.print_exc()

If you want more control over the exception info - for example to put it
in a string instead of printing it - look at the other functions in the
traceback module.

Kent
Dec 10 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Chris Maloof | last post: by
1 post views Thread by Tosch | last post: by
3 posts views Thread by per9000 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.