【问题标题】:Handle specific exception type in python在python中处理特定的异常类型
【发布时间】:2010-12-01 21:42:29
【问题描述】:

我有一些处理异常的代码,并且我只想在它是特定异常时执行特定的操作,并且只能在调试模式下执行。比如:

try:
    stuff()
except Exception as e:
    if _debug and e is KeyboardInterrupt:
        sys.exit()
    logging.exception("Normal handling")

因此,我不想只添加:

except KeyboardInterrupt:
    sys.exit()

因为我试图将调试代码中的差异保持在最小程度

【问题讨论】:

    标签: python exception exception-handling


    【解决方案1】:

    嗯,真的,您可能应该将KeyboardInterrupt 的处理程序分开。为什么您只想在调试模式下处理键盘中断,而在其他情况下吞下它们?

    也就是说,您可以使用isinstance 来检查对象的类型:

    try:
        stuff()
    except Exception as e:
        if _debug and isinstance(e, KeyboardInterrupt):
            sys.exit()
        logger.exception("Normal handling")
    

    【讨论】:

    • 这是在没有外壳的嵌入式系统上运行的。所以我们只能在调试模式下获得键盘中断。所以我们只处理所有异常。
    【解决方案2】:

    要么使用其他答案中提到的标准方法,要么如果你真的想在 except 块中进行测试,那么你可以使用isinstance()

    try:
        stuff()
    except Exception as e:
       if _debug and isinstance(e, KeyboardInterrupt):
            sys.exit()
        logging.exception("Normal handling")
    

    【讨论】:

      【解决方案3】:

      这几乎就是它的完成方式。

      try:
          stuff()
      except KeyboardInterrupt:
          if _debug:
              sys.exit()
          logging.exception("Normal handling")
      except Exception as e:
          logging.exception("Normal handling")
      

      重复次数最少。然而,不是零,而是最小的。

      如果“正常处理”多于一行代码,可以定义一个函数,避免两行代码重复。

      【讨论】:

      • 你比我快 30 秒,我想
      • @marcog: except Exception as e 在 Python 2.6+ 中有效。
      【解决方案4】:

      您应该让 KeyboardInterrupt 一直冒泡并将其困在最高级别。

      if __name__ == '__main__':
          try:
              main()
          except KeyboardInterrupt:
              sys.exit()
          except:
              pass
      
      def main():
          try:
              stuff()
          except Exception as e:
              logging.exception("Normal handling")
              if _debug:
                  raise e
      

      【讨论】:

        【解决方案5】:

        怎么了

        try:
            stuff()
        except KeyboardInterrupt:
            if _debug:
                logging.exception("Debug handling")
                sys.exit()
            else:
                logging.exception("Normal handling")
        

        【讨论】:

          【解决方案6】:

          您可以在 Python 中命名特定的异常:

          try:
              stuff()
          except KeyboardInterrupt:
              sys.exit()
          except Exception:
              normal_handling()
          

          【讨论】:

            【解决方案7】:
            try:
                stuff()
            except KeyboardInterrupt:
                if _debug:
                    sys.exit()
                logging.exception("Normal handling")
            except ValueError:
                if _debug:
                    sys.exit()
                logging.exception("Value error Normal handling")
            else:
                logging.info("One more message without exception")
            

            【讨论】:

            • -1: 'else' 应该是 'except','else' 子句是在没有异常时执行的代码。
            • 是的,'else'分支会在没有异常的情况下执行。这只是一个例子。但你是对的,我应该编辑代码示例以显示“真实”程序状态。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-11
            • 1970-01-01
            相关资源
            最近更新 更多