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

如何在不需要任何操作的情况下处理Python异常

  •  0
  • Jon  · 技术社区  · 6 年前

    在python中,以下语法通常是处理异常的方法 SystemExit 是可能的。

    try:
        execute some command here
    except SystemExit:
        take a different course of action here
    

    然而,我想以一种不采取其他行动的方式使用异常处理。总的来说,我只是想防止 系统退出 并继续执行程序。我尝试了以下方法,但它只在for循环中起作用

    try:
        execute some command here
    except SystemExit:
        continue
    

    如何执行此常规代码块?

    1 回复  |  直到 6 年前
        1
  •  1
  •   David Loungani    6 年前

    尝试使用pass语句:

    try:
        execute some command here
    except SystemExit:
        pass