引发异常时是否可以自动闯入调试器? [英] Is it possible to automatically break into the debugger when a exception is thrown?

查看:83
本文介绍了引发异常时是否可以自动闯入调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人在最​​初抛出的函数之外捕获到异常,则有人将无法访问本地堆栈。结果,无法检查可能导致异常的变量的值。

If ones catches an exception outside of the function it is originally thrown, ones loses access to the local stack. As a result one cannot inspect the values of the variables that might have caused the exception.

是否有一种方法可以随时自动启动调试器( import pdb; pdb.set_trace()

Is there a way to automatically start break into the debugger (import pdb; pdb.set_trace()) whenever a exception is thrown to inspect the local stack?

推荐答案

我发现我在寻找使用Python pdb检查未处理异常原因的最简单方法是什么?


用以下代码包装:

Wrap it with that:

<!-- language: lang-py -->
def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                pdb.post_mortem(sys.exc_info()[2])
        return wrapper
    return decorator

示例:

@debug_on(TypeError)
def buggy_function()
    ....
    raise TypeError


这篇关于引发异常时是否可以自动闯入调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆