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