Escaping try-except blocks for Python debugging

Aug 25, 2008 16:21

Often you need to disable try/except blocks while debugging code because you want to find out the point of occurrence of the exception and other info which is swallowed by the block that catches the exception. With languages that use braces for block demarcation, it's as easy as simply commenting out the try and catch/except lines, minding the ( Read more... )

blocks, try, indentation, code, debugging, except, python

Leave a comment

Comments 1

or..... anonymous September 11 2008, 20:48:08 UTC
you could do this...

import traceback

try:
some_code
some_more_code
except:
some_code
print traceback.format_exc()

The last line in the code will print the traceback to the exception. The advantage of using traceback is that exceptions can be logged in prod/dev. enviornment. Comment the line, when the details of exceptions are not required.

Regards,
Mohit Ranka
http://www.linkedin.com/in/mohitranka

Reply


Leave a comment

Up