代码之家  ›  专栏  ›  技术社区  ›  matanster

如何捕获python 3中除一组异常之外的任何异常

  •  3
  • matanster  · 技术社区  · 7 年前

    SyntaxError AssertionError

    动机是我们想要迭代大量的输入文件,并使用 try ,请

    在更深层的层面上,除了Python3的异常层次结构之外,是否还有任何现有的异常分组能够更好地区分异常类型?我认为给定的层次结构并不十分合理。

    2 回复  |  直到 7 年前
        1
  •  4
  •   Anatolii    7 年前

    try:
        # do some stuff here
    except SyntaxError:
        # do not catch this one
        raise
    except AssertionError:
        # do not catch this one
        raise
    except Exception:
        # catch all the others
    
        2
  •  1
  •   Codebender    7 年前

    enter code here
    try:
        #code
    except Exception:
        if Exception == AssertionError:
            # do smthg
        elseif Exception == SyntaxError:
            # do smthg
        else:
            return