user3110379
user3110379

Reputation: 159

python how to intentionally leave indented line blank?

for line in sys.stdin:
    line = line.strip()
    uid, profile = line.split('\t')
    try:
        process(profile)
    except:
        # do nothing and skip this profile

I would like to skip all profiles that throw exceptions. But python gives me

IndentationError: expected an indented block

at the line "# do nothing and skip this profile".

How do I tell python to do nothing?

Upvotes: 3

Views: 138

Answers (2)

user2555451
user2555451

Reputation:

Use the pass keyword:

except:
    pass

pass is basically a do-nothing placeholder.


Also, it is generally considered a bad practice to have a bare except like that. As explained here, it will catch all exceptions, even those that are unrelated.

Instead, you should catch a specific exception:

except Exception:

Upvotes: 3

Adam Smith
Adam Smith

Reputation: 54183

The Python "NOP" is pass

...
except:
    pass

That said, never use bare except clauses. They catch things like a KeyboardInterrupt that you (almost) absolutely NEVER want to handle yourself. If there's nothing SPECIFIC you're catching, do except Exception

Upvotes: 4

Related Questions