如何在已处理的异常上停止 PyCharm 的 break/stop/halt 功能(即仅在 python 未处理的异常上中断)?

How to stop PyCharm's break/stop/halt feature on handled exceptions (i.e. only break on python unhandled exceptions)?

我知道 PyCharm 正在暂停我所有的异常,即使是我在 try except 块中处理的异常。我不希望它在那里中断 - 我正在处理并且可能期待一个错误。但是我确实希望它停止并暂停执行的所有其他异常(例如,以便我拥有程序状态并对其进行调试)。

如何做到这一点?

我尝试进入 python 异常断点选项,但我没有看到像“仅在未处理的异常时中断”这样的选项,例如这些建议:

注意这是我的当前状态,请注意它是如何在我的 try 块中停止的...:(

交叉发布:https://intellij-support.jetbrains.com/hc/en-us/community/posts/4415666598546-How-to-stop-PyCharm-s-break-stop-halt-feature-on-handled-exceptions-i-e-only-break-on-python-unhandled-exceptions-


我试过了:

In your link here intellij-support.jetbrains.com/hc/en-us/community/posts/… the poster Okke said they solved this issue adding --pdb to the 'addition arguments', which someone later said they probably meant interpreter options.

但没有成功出现错误:

/Users/brandomiranda/opt/anaconda3/envs/meta_learning/bin/python --pdb /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --cmd-line --multiproc --qt-support=auto --client 127.0.0.1 --port 58378 --file /Users/brandomiranda/ultimate-utils/tutorials_for_myself/try_catch_pycharm_issues/try_catch_with_pickle.py
unknown option --pdb
usage: /Users/brandomiranda/opt/anaconda3/envs/meta_learning/bin/python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

Process finished with exit code 2

我认为它实际上已经在工作了,但实际上您没有捕捉到正确的错误。在你的代码中你有:

try:
    pickle.dumps(obj)
except pickle.PicklingError:
    return False

但是抛出的错误是AttributeError。所以为了避免你需要这样的东西:

try:
    pickle.dumps(obj)
except (pickle.PicklingError, AttributeError):
    return False

不确定为什么 pycharm 在 except 错误的情况下会按照它的方式行事,但这行得通:

def is_picklable(obj: Any) -> bool:
    """
    Checks if somehting is pickable.

    Ref:
        - 
        - pycharm halting all the time issue: 
    """
    import pickle
    try:
        pickle.dumps(obj)
    except:
        return False
    return True

整个文件:

"""
trying to resolve:
- https://intellij-support.jetbrains.com/hc/en-us/requests/3764538


You will need to run
    pip install transformers
    pip install fairseq

On mac
    pip3 install torch torchvision torchaudio
on linux
    pip3 install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
on windows
    pip3 install torch torchvision torchaudio
"""
import argparse
from argparse import Namespace
from typing import Any

from fairseq import optim
from torch import nn
from torch.optim import Optimizer
from torch.optim.lr_scheduler import _LRScheduler
from transformers.optimization import AdafactorSchedule


def invoke_handled_exception():
    try:
        1 / 0
    except ZeroDivisionError:
        print('exception caught')


def make_args_pickable(args: Namespace) -> Namespace:
    """
    Returns a copy of the args namespace but with unpickable objects as strings.

    note: implementation not tested against deep copying.
    ref:
        - 
        - pycharm halting all the time issues: 
        - stop progressbar from printing progress when checking if it's pickable: 
    """
    pickable_args = argparse.Namespace()
    # - go through fields in args, if they are not pickable make it a string else leave as it
    # The vars() function returns the __dict__ attribute of the given object.
    for field in vars(args):
        # print(f'-----{field}')
        field_val: Any = getattr(args, field)
        if not is_picklable(field_val):
            field_val: str = str(field_val)
        # - after this line the invariant is that it should be pickable, so set it in the new args obj
        setattr(pickable_args, field, field_val)
        # print('f-----')
    return pickable_args


def is_picklable(obj: Any) -> bool:
    """
    Checks if somehting is pickable.

    Ref:
        - 
        - pycharm halting all the time issue: 
    """
    import pickle
    try:
        pickle.dumps(obj)
    except:
        return False
    return True


def invoke_handled_exception_brandos_pickle_version():
    mdl: nn.Module = nn.Linear(4, 3)
    optimizer: Optimizer = optim.adafactor.Adafactor(params=mdl.parameters())
    scheduler: _LRScheduler = AdafactorSchedule(optimizer)
    args: Namespace = Namespace(scheduler=scheduler, optimizer=optimizer, model=mdl)
    make_args_pickable(args)
    print('Success if this line printed! Args was made into a pickable args without error')


# -- tests
invoke_handled_exception()
invoke_handled_exception_brandos_pickle_version()