This question already has an answer here:
I have a try...except block in my code and When an exception is throw. I really just want to continue with the code because in that case, everything is still able to run just fine. The problem is if you leave the except: block empty or with a #do nothing, it gives you a syntax error. I can't use continue because its not in a loop. Is there a keyword i can use that tells the code to just keep going?
except:
pass
The standard "nop" in Python is the pass
statement:
try:
do_something()
except Exception:
pass
Using except Exception
instead of a bare except
avoid catching exceptions like SystemExit
, KeyboardInterrupt
etc.
Because of the last thrown exception being remembered in Python 2, some of the objects involved in the exception-throwing statement are being kept live indefinitely (actually, until the next exception). In case this is important for you and (typically) you don't need to remember the last thrown exception, you might want to do the following instead of pass
:
try:
do_something()
except Exception:
sys.exc_clear()
This clears the last thrown exception.
In Python 3, the variable that holds the exception instance gets deleted on exiting the except
block. Even if the variable held a value previously, after entering and exiting the except
block it becomes undefined again.
exc_clear
was removed in python 3. docs.python.org/3/whatsnew/3.0.html#index-22. For some ways to address this in Python 3 see here: cosmicpercolator.com/2016/01/13/… - bcattle
There's a new way to do this coming in Python 3.4:
from contextlib import suppress
with suppress(Exception):
# your code
Here's the commit that added it: http://hg.python.org/cpython/rev/406b47c64480
And here's the author, Raymond Hettinger, talking about this and all sorts of other Python hotness (relevant bit at 43:30): http://www.youtube.com/watch?v=OSGv2VnC0go
If you wanted to emulate the bare except
keyword and also ignore things like KeyboardInterrupt
—though you usually don't—you could use with suppress(BaseException)
.
Edit: Looks like ignored
was renamed to suppress
before the 3.4 release.
try...catch: pass
, so if an exception is raised inside the block, execution will continue after the end of the block. - Jack O'Connorwith suppress(TypeError): return data[0]
(longer example: pastebin.com/gcvAGqEP) - Airwith suppress(Exception)
on 2017-09 , because try/except performs better. Check this commits Reverted "Fixed #27818 -- Replaced try/except/pass with contextlib.su… - stackoverYC
Try this:
try:
blah()
except:
pass
while True:
try:
f = open('filedoesnotexist.txt')`except:
pass
KeyboardInterrupt stops and exits the code. - Chthonic Projectexcept
will catch any exception, including a KeyboardInterrupt, but only if it happens inside thetry
. In your example there, a KeyboardInterrupt can occur before thetry
or inside theexcept
, where it won't be caught. If you run an example likewhile True:
try: pass
except: pass
, you'll find that the KeyboardInterrupt gets caught just about 50% of the time. If youtime.sleep(1)
inside thetry
, you'll find that it gets caught almost every time. - Jack O'Connor