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

问题描述 投票:1回答:1

在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)}')

并且在调试中产生跟随输出

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异常。这是我的断点设置btw

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和ExitGenertor

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

例如with breakpoint condition

在Pycharm中,我想在调试模式下,停止输入代码的任何异常,但忽略库函数引发和捕获的所有异常。 Pycharm在断点处有一个选项...

python debugging exception pycharm
1个回答
0
投票

更新

© www.soinside.com 2019 - 2024. All rights reserved.