Catching error text like that shown in console?

  • Thread starter Peter A. Schott
  • Start date
P

Peter A. Schott

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
 
G

gene tani

Peter said:
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-newstreams.html#os-newstreams
http://docs.python.org/lib/module-subprocess.html
http://mail.python.org/pipermail/python-list/2005-October/305671.html

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

Larry Bates

Peter said:
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
 
K

Kent Johnson

Peter said:
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
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top