How to write an empty indentation block in Python?

前端 未结 2 619
迷失自我
迷失自我 2020-12-06 03:56

The runtime keeps telling me:

expected an indented block

But I don\'t want write nothing inside my except block, I just want it

相关标签:
2条回答
  • 2020-12-06 04:23

    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.

    0 讨论(0)
  • 2020-12-06 04:38

    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.

    0 讨论(0)
提交回复
热议问题