Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

syntax - How to write an empty indentation block in Python?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...