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

Fuz*_*ury 2 python debugging exception pycharm

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

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

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

参见例如这个程序

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)}')
Run Code Online (Sandbox Code Playgroud)

并在调试中产生以下输出

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
Run Code Online (Sandbox Code Playgroud)

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

Pycharm 任何 Igoore 库文件异常

此外,几年前的这似乎与所有异常的 Break相关,除非它是停止迭代或 Generator Exit

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

Fuz*_*ury 5

更新

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

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

not (isinstance(__exception__ , tuple) and len(__exception__)>=1 and __exception__[0] in [StopIteration, GeneratorExit])
Run Code Online (Sandbox Code Playgroud)

带断点条件