52

I have some code like:

try:
    do_the_first_part()
except SomeError:
do_the_next_part()

I get an error that says "expected an indented block" - but I don't want to write anything inside my except block, I just want it to catch and swallow the exception. How can I do this in Python?


See also: How to use "pass" statement?

2
  • What Python tutorial are you following? Where are you trying to learn Python from? Commented Oct 7, 2009 at 10:24
  • I didn't follow any tutorial. All I know I got from the AppEngine documentation Commented Oct 7, 2009 at 17:33

3 Answers 3

113

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Be careful with using that idiom as expressed above. A generic "except" will catch any exception, including many programming errors like referencing undefined variables, etc, and the empty clause will just swallow the exceptions. You can hide serious problems this way.
A better example idiom might be "while condition-with-side-effects : pass". That said, conditions with side-effects can have an odour too.
Exceptions are very expensive. Use them wisely.
Austin: Wher do you have that wisdom from? Updated it with Python? Python Exceptions aren't terribly expensive, and you should use them when appropriate.
@u0b34a0f6ae exceptions are horribly expensive in .NET.
|
0

I've never done this in more permanent code, but I frequently do it as a placeholder

if some_expression:
  True
else:
  do_something(blah)

Just sticking a True in there will stop the error. Not sure if there's anything bad about this.

4 Comments

There's nothing wrong with that technically, but these situations is why 'pass' exists in the first place.
it is bad only since it throws readers off, it's absolutely not idiomatic.
I prefer Ture to Pass - because the truth shall set you free ;)
why not if not some_expression: do_something(blah)? Two fewer lines.
0

For those who are very unclear as to why you would want to do this. Here is an example where I initially thought that an empty block would be a good idea:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, (Path, str)):
        debug_dir = debug_dir # this is my null operation
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    else:
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))

But it would be more clear to reorganize the statement:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    elif not isinstance(debug_dir, (Path, str)):
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.