即使在 python 中的 try catch 中,如何让 vscode 调试器停止?

Cha*_*ker 5 visual-studio python-3.x visual-studio-code vscode-debugger

看到建议为“相似”的问题,似乎大多数人想要的与我想要的相反。我想要的是 Vs-code 的调试器在错误点停止,即使在 try catch 中(通常会这样做)。

但是,当在像这样的 try catch 中时,它所做的并不是停止:

import traceback 

try:
    main() # has bugs I'm trying to debug
except Exception as e:
    send_email(traceback.format_exc())
    send_email(e)
Run Code Online (Sandbox Code Playgroud)

我知道这可能是一件奇怪的事情,并且 vs-code 的调试器可能运行正确(因为我的代码告诉它如何处理异常!)但我有一些错误,我想调试而不是捕捉。事实上,我的外部 try catch 就在那里,因为我正在使用一个集群,当有任何错误时,它会向我发送电子邮件并告诉我有关它们的信息。否则我就不会在我的主要代码周围有一个 try catch 。

当我实际调试时,有没有办法告诉 vs-code 忽略我的尝试捕获?


我在写这篇文章时的一个想法是改变我捕捉到的异常类型……虽然在调试期间我希望它总是停止,而在不调试时我希望它永远不会停止并向我发送一封包含漏洞。

有任何想法吗?


新错误:

Exception has occurred: TypeError
catching classes that do not inherit from BaseException is not allowed
Run Code Online (Sandbox Code Playgroud)

使用我的EmptyException答案时:

class EmptyError(Exception):
    def __init__(self):
        pass
Run Code Online (Sandbox Code Playgroud)

Cha*_*ker 4

这可能是一种非常愚蠢的方法,但是当我在调试模式下运行时,我有一个标志设置为 true:

args.debug = True
Run Code Online (Sandbox Code Playgroud)

然后根据该标志我设置异常类型:

if args.debug:
    args.ExceptionType = Exception
else:
    args.ExceptionType = EmptyError
Run Code Online (Sandbox Code Playgroud)

其中EmptyError有一个不执行任何操作的自定义异常,它只是让 VS-code 在我以调试模式运行时停止:

class EmptyError(Exception):
    def __init__(self):
        pass
Run Code Online (Sandbox Code Playgroud)

有点愚蠢,但我不知道还有什么更好的。