python - Pycharm - 中断任何异常但忽略 StopIteration 和 ExitGenerator

标签 python debugging exception pycharm

在 Pycharm 中,我想在 Debug模式下,停止进入我的代码的任何异常,但忽略由库函数抛出和捕获的任何异常。

Pycharm 在断点中有一个名为 Any Exception 的选项,您可以在其中说“On Raise”和“Ignore library files”,这有很长的路要走,但它不会忽略 StopIteration 和 ExitGenerator,这意味着它会在任何生成器或产量声明。

例如在生成器下方的代码中 next((x for x in a_list))抛出一个 ExitGenerator 异常,Pycharm 在 Debug模式下停止该异常,但这实际上是由库代码捕获和处理的,所以我想忽略它。

参见例如这个程序

import pandas as pd

try:
    # spurious exception
    a_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    first_item = next((x for x in a_list))
    print(f'first item = {first_item}')

except Exception as e:
    # the program won't go here as the ExitGenerator exception is handled by the standard library
    print(f'got exception from generator : {str(e)}')

try:
    # proper exception from a library
    df = pd.DataFrame(index=[1, 2, 3], data=['a', 'b', 'c'], columns=['letters'])
    # try to access but use the wrong column name to generate an exception
    print(df['non_existent_column'])

except Exception as e:
    # the program will come here as the code tried to access a non-existent column
    print(f'got exception from pandas : {str(e)}')

并在调试中产生以下输出
Connected to pydev debugger (build 201.6668.115)
Stack: 
    <genexpr>, play.py:6
    <module>, play.py:6
first item = a
Stack: 
    <module>, play.py:17
got exception from pandas : 'non_existent_column'

Process finished with exit code 0

Pycharm 首先捕获未到达我的代码的虚假生成器异常,然后捕获读取我的代码的正确的 Pandas 异常。这是我的断点设置顺便说一句

Pycharm Any Exception with Igoore Library Files

几年前的这似乎也是相关的Break on all exceptions except if it is stop iteration or Generator Exit

表明它可能已经解决,但它不知道如何启用它。

最佳答案

更新

我已经根据这里的一些答案设法使一些工作get-last-exception-in-pdb

如果我将此添加到 Pycharm 条件中,它会避免忽略 StopIteration 和 ExitGenerator

not (isinstance(__exception__ , tuple) and len(__exception__)>=1 and __exception__[0] in [StopIteration, GeneratorExit])

with breakpoint condition

关于python - Pycharm - 中断任何异常但忽略 StopIteration 和 ExitGenerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61363775/

相关文章:

ios - 通过 VPN 连接的 iPhone 远程调试

c - STM32闪烁代码仅在进入 Debug模式时有效

c++ - 类型转换和异常处理

c++ - 异常后重置 Cuda 上下文

python - 使用私有(private)成员对类进行子类化

python - 如何使用 lxml 从此链接中抓取历史价格?

python - 在 python 中创建时扩展列表,如何?

c++ - 如何在 Eclipse 中启动 gdb 调试之前运行 linux 脚本

java - 严重 : Missing dependency for field : private org. glassfish.jersey.server.wadl.WadlApplicationContext

python - 如何删除使用 tempfile.mkdtemp 创建的目录?