search

Home  >  Q&A  >  body text

python 的异常处理 循环

try:
  语句A
except xxx:
  语句A

要是再出现异常怎么循环下去?

PHPzPHPz2888 days ago314

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 17:42:38

    Try again in except.

    try:
        语句A
    except Exception as e:
        try:
            语句A
        except Exception as e:
            # 继续

    But this is stupid. If you really want to implement an infinite loop to catch exceptions, write like this:

    def func():
        try:
            语句A
        except Exception as e:
            return False
        else:
            return True
            
    while not func():
        pass    # Or do something

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:42:38

    According to the way you write it, the error should be ignored and statement A should be executed?
    Written specifically by yourself.

    循环:
        try:
           语句A
        except:
           pass  #这是忽略错误
        

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:42:38

    Why is there such a strange demand? You should consider changing the direction. This is obviously an endless loop

    reply
    0
  • Cancelreply