Framester Framester - 1 month ago 5x
Python Question

Is it possible to automatically break into the debugger when a exception is thrown?

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.

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?


Thanks for your two answers, I +1ed you, but I found a different solution.


I found what I was looking for in an answer to What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

Wrap it with that:

<!-- language: lang-py -->
def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        def wrapper(*args, **kwargs):
                return f(*args, **kwargs)
            except exceptions:
        return wrapper
    return decorator


def buggy_function()
    raise TypeError