#!/usr/local/bin/python3.1 """Adapted from example from: http://docs.python.org/3.1/library/traceback.html 2010-02-24 Demonstrates the different ways to print and format the exception and traceback. Corrected for proper use of "tb_lineno". """ import sys, traceback def lumberjack(): bright_side_of_death() def bright_side_of_death(): return tuple()[0] try: lumberjack() except: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() print("*** print_tb:") traceback.print_tb(exceptionTraceback, limit=1, file=sys.stdout) print("*** print_exception:") traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=2, file=sys.stdout) print("*** print_exc:") traceback.print_exc() print("*** format_exc, first and last line:") formatted_lines = traceback.format_exc().splitlines() print(formatted_lines[0]) print(formatted_lines[-1]) print("*** format_exception:") print(repr(traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback))) print("*** extract_tb:") print(repr(traceback.extract_tb(exceptionTraceback))) print("*** format_tb:") print(repr(traceback.format_tb(exceptionTraceback))) #print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback)) print("*** tb_lineno:", exceptionTraceback.tb_lineno)