[Tutor] Trapping High-Level Exceptions As Strings
Tim Johnson
tim at johnsons-web.com
Sun Sep 12 18:53:21 CEST 2004
By "High-Level", I mean using the except label
without any error object as a parameters, so
that I may trap any error that "falls" through
more structured traps.
Currently, I'm using something like this
code fragment below:
###############################################################
except PrjError, err:
print "AN INTERNAL PROGRAMMING ERROR WAS FOUND:"
print "Below is the report:\n"
print err.err
exit_code = 1
except HaltError, err:
print "A PROGRAM TERMINATION HAS OCCURRED:"
print "REASON: %s\n" % (err.err)
exit_code = 1
except:
print "WE HAVE ERRORS: Report the information below to your technical support"
print "----------------------------------------------------------------------------------------"
exit_code = 1
traceback.print_exc()
###############################################################
In the first two examples, I'm initializing an error object and
obtaining a string which I can print, or write to a logfile at
will.
In the last example, I'm using a call to traceback.print_exc(),
which does the printing itself. The traceback information is
very good, and I want to trap that also. If I have a divide by
zero error, traceback gives me the following:
###############################################################
Traceback (most recent call last):
File "./nwmls_cron.py", line 98, in ?
process(args)
File "./nwmls_cron.py", line 16, in process
1 / 0 # test exception handling and logging
ZeroDivisionError: integer division or modulo by zero
integer division or modulo by zero
###############################################################
Which is very helpful, but I don't know how to obtain that
message as a string.
Using
err_msg = format_list(extract_tb())
print err_msg
# gets a string for me to print, write to file
# or both, I presume, but I only get the last
# line. Example:
"integer division or modulo by zero"
So how may I get more information, which can point
to the origin of the error?
help(traceback) indicates that extract_tb() has an
optional "tb" parameter, but I'm not clear on
how to use it.
TIA
Tim
--
Tim Johnson <tim at johnsons-web.com>
http://www.alaska-internet-solutions.com
More information about the Tutor
mailing list