Tuesday, January 06, 2009

Python stack trace

Outputting the stack trace can be immensely useful in certain situations.


import traceback, sys

def bar():
raise Exception('Booboo')

def foo():
bar()

try:
foo()
except:
traceback.print_exc()
print 'Got here.'


Result:

Traceback (most recent call last):
File "./foo.py", line 14, in
foo()
File "./foo.py", line 11, in foo
bar()
File "./foo.py", line 8, in bar
raise Exception('Booboo')
Exception: Booboo
Got here.

No comments:

Post a Comment