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 braces at the same time. Doing that with Python is not possible because you also need to un-indent the enclosed lines of code. I just figured a simple way of working around it. Change this:

try:
some_code
more_code
except:
some_more_code

to this:

if True:#try:
some_code
more_code
if False:#except:
some_more_code

Hope you find this useful.

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

Previous post Next post
Up