How can I quickly disable a try statement in python for testing?
Question:
Say I have the following code:
try:
print 'foo'
# A lot more code...
print 'bar'
except:
pass
How would I for testing purposes disable the try-statement temporary?
You can’t just comment the try
and except
lines out as the indention will still be off.
Isn’t there any easier way than this?
#try:
print 'foo'
# A lot more code...
print 'bar'
#except:
# pass
Answers:
Turn it into an if True
statement, with the except
clause ‘commented out’ by the else
branch (which will never be executed):
if True: # try:
# try suite statements
else: # except:
# except suite statements
The else:
is optional, you could also just comment out the whole except:
suite, but by using else:
you can leave the whole except:
suite indented and uncommented.
So:
try:
print 'foo'
# A lot more code...
print 'bar'
except SomeException as se:
print 'Uhoh, got SomeException:', se.args[0]
becomes:
if True: # try:
print 'foo'
# A lot more code...
print 'bar'
else: # except SomeException as se:
print 'Uhoh, got SomeException:', se.args[0]
Make your except
only catch something that the try
block won’t throw:
class FakeError:
pass
try:
# code
except FakeError: # OldError:
# catch
Not actually sure if this is a good idea, but it does work!
You could reraise the exception as the first line of your except block, which would behave just as it would without the try/except.
try:
print 'foo'
# A lot more code...
print 'bar'
except:
raise # was: pass
Piggy-backing off of velotron’s answer, I like the idea of doing something like this:
try:
print 'foo'
# A lot more code...
print 'bar'
except:
if settings.DEBUG: # Use some boolean to represent dev state, such as DEBUG in Django
raise # Raise the error
# Otherwise, handle and move on.
# Typically I want to log it rather than just pass.
logger.exception("Something went wrong")
Say I have the following code:
try: print 'foo' # A lot more code... print 'bar' except: pass
How would I for testing purposes disable the try-statement temporary?
You can’t just comment the try
and except
lines out as the indention will still be off.
Isn’t there any easier way than this?
#try: print 'foo' # A lot more code... print 'bar' #except: # pass
Turn it into an if True
statement, with the except
clause ‘commented out’ by the else
branch (which will never be executed):
if True: # try:
# try suite statements
else: # except:
# except suite statements
The else:
is optional, you could also just comment out the whole except:
suite, but by using else:
you can leave the whole except:
suite indented and uncommented.
So:
try:
print 'foo'
# A lot more code...
print 'bar'
except SomeException as se:
print 'Uhoh, got SomeException:', se.args[0]
becomes:
if True: # try:
print 'foo'
# A lot more code...
print 'bar'
else: # except SomeException as se:
print 'Uhoh, got SomeException:', se.args[0]
Make your except
only catch something that the try
block won’t throw:
class FakeError:
pass
try:
# code
except FakeError: # OldError:
# catch
Not actually sure if this is a good idea, but it does work!
You could reraise the exception as the first line of your except block, which would behave just as it would without the try/except.
try:
print 'foo'
# A lot more code...
print 'bar'
except:
raise # was: pass
Piggy-backing off of velotron’s answer, I like the idea of doing something like this:
try:
print 'foo'
# A lot more code...
print 'bar'
except:
if settings.DEBUG: # Use some boolean to represent dev state, such as DEBUG in Django
raise # Raise the error
# Otherwise, handle and move on.
# Typically I want to log it rather than just pass.
logger.exception("Something went wrong")