How to write an empty indentation block in Python?

Jader Dias picture Jader Dias · Oct 7, 2009 · Viewed 25.5k times · Source

The runtime keeps telling me:

expected an indented block

But I don't want write nothing inside my except block, I just want it to catch and swallow the exception.

Answer

Peter picture Peter · Oct 7, 2009

Just write

pass

as in

try:
    # Do something illegal.
    ...
except:
    # Pretend nothing happened.
    pass

EDIT: @swillden brings up a good point, viz., this is a terrible idea in general. You should, at the least, say

except TypeError, DivideByZeroError:

or whatever kinds of errors you want to handle. Otherwise you can mask bigger problems.