简体   繁体   中英

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?

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM