如何在PyDev中启用事后调试?

5
我希望我的程序在出现未处理的异常时能够进入交互式控制台模式,但是我不知道如何实现。目前情况下,异常会被报告并立即终止进程。
经过一些搜索,我找到了这篇文章:http://sourceforge.net/tracker/index.php?func=detail&aid=3029746&group_id=85796&atid=577332,该文建议使用pydevd.set_pm_excepthook()。
然而,当我添加以下内容时:
import pydevd
pydevd.set_pm_excepthook()

当我运行我的代码时,出现了异常:

This function is now replaced by GetGlobalDebugger().setExceptHook and is now controlled by the PyDev UI.')
DeprecationWarning: This function is now replaced by GetGlobalDebugger().setExceptHook and is now controlled by the PyDev UI.

但是:

GetGlobalDebugger().setExceptHook()

看起来不起作用,GetGlobalDebugger()在全局命名空间中不存在。

我找到了答案,但似乎我不能在8小时内回答自己的问题,所以我想我应该等待。 - Sebastian Jansson
2个回答

4
实际上,您不需要以编程方式完成此操作... 您可以进入Debug视角 > Pydev菜单 > 管理异常断点。

GUI example

并勾选“未捕获异常时挂起”。在大多数情况下,您希望捕获所有类型的异常(因此选择“全选”),但您也可以选择单独管理异常。

Dialog


哦,这很方便。我以为应该有这样的选项,但在首选项或调试配置中没有找到它。谢谢你的提示。 - Sebastian Jansson
这是用什么编程语言写的程序? - Seanny123
2
“未捕获异常挂起”与pdb.post_portem(tb)不同,后者只能在特定情况下调用,并受应用程序特定逻辑的控制。您能否扩展您的答案,提供有关在PyDev.Debugger中等效于pdb.post_portem(tb)的信息,以便所有希望以编程方式执行此操作的人使用? - Piotr Dobrogost
我想补充一下Fabio的回答,你必须明确指定将被捕获的异常类型(在“Suspend on uncaught exceptions”上方有一个长复选框列)。否则,在异常发生后,主控制台将终止,交互式控制台(在主控制台下方)也无法工作(当你输入内容时,会显示*[Invalid Frame]:请选取帧以连接控制台。)。 - Stockos

2

好的,经过一段时间的思考,我明白了一个显而易见的问题,代码应该是:

import pydevd
pydevd.GetGlobalDebugger().setExceptHook(Exception, True, False)

为了捕获任何未处理的异常,该方法可以在程序崩溃时进入调试模式,并且可以以其他方式使用,如setExceptHook文档所述:

Should be called to set the exceptions to be handled and whether it should break on uncaught and caught exceptions.

Can receive a parameter to stop only on some exceptions.

    E.g.:
        set_pm_excepthook((IndexError, ValueError), True, True)

        or

        set_pm_excepthook(IndexError, True, False)

        if passed without a parameter, will break on any exception

    @param handle_exceptions: exception or tuple(exceptions)
        The exceptions that should be handled.

    @param break_on_uncaught bool
        Whether it should break on uncaught exceptions.

    @param break_on_caught: bool
        Whether it should break on caught exceptions.

我希望这篇文章能帮助那些想在eclipse中使用pydev调试器来调试程序异常的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接