python - 如何在Python中记录缩进错误?

标签 python python-2.7 debugging error-handling try-except

嗨,我正在尝试在函数 test() 中记录缩进错误。但是当我运行代码时,它的执行没有任何错误。谁能告诉我原因。

def test():
        try:                        //1
                        print "ABC" //2//indentaton error//
                logger.info("printed")//3
        #except Exception as e://
                #logger.error("Exception occured while order dictionary",exc_info=True)
        except IndentationError as e://4
                logger.error("indentation err",exc_info=True)//5

最佳答案

Python 在开始执行任何代码之前会解析整个代码文件。不幸的是,IndentationErrorSyntaxError的子类,它是一个只能从evalexec捕获的错误,或 import 语句。

出于测试目的,您可以将一些损坏的代码写入单独的文件中,并尝试将该文件作为模块导入。这样您就可以捕获 IndentationError(如果这与您相关)。

关于python - 如何在Python中记录缩进错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53809134/

相关文章:

ruby-on-rails - 随着时间的推移脱轨的基准内存失败

python - CPU 在 python 中使用 select 达到 100% 使用率

python - 评估列表python中的变量

python - Python 采用什么版本的 C?

python - 在 Python 中导入按顺序命名的多个文件

delphi - 为什么我的 Delphi 附加 DLL 中的 DebugHook 没有设置为 1?

python - opencv.groupRectangles() c++ 和 python 层之间的差异

python - 如何将我的自定义菜单添加到门户用户菜单列表?

python - 为什么 GAE python SDK 在 vi​​rtualenv 中使用系统的 python?

javascript - 如何在 Google Chrome 扩展中获取 Node.js 进程的调试目标信息?