2

In VS.Code with the Python extension, I can break on "Uncaught Exceptions" and "Raised Exceptions".

But what if I don't want to break on all exceptions, only those that are uncaught in a certain function? (but which are caught, say by a framework like FastAPI, in a shallower frame?)

If debugging on the console, when I know there is a function that has a line causing an exception, I can change it from something like this:

def some_function():
    line_causing_some_exception()

To this:

def some_function():
    try:
        line_causing_some_exception()
    except:
        import pdb
        pdb.post_mortem()
        # Let the framework handle the exception as usual:
        raise

I can even debug graphically, instead of depending on the console, by using the excellent wdb, which is very handy for server apps, or for when I'm not launching the python code directly and have no access to its console:

def some_function():
    try:
        line_causing_some_exception()
    except:
        import wdb
        wdb.post_mortem()
        # Let the framework handle the exception as usual:
        raise

In VS.Code, if I add a breakpoint after catching the exception, I don't have the "post mortem" traceback, only the current frame:

def some_function():
    try:
        line_causing_some_exception()
    except:
        import debugpy
        debugpy.breakpoint()
        # Let the framework handle the exception as usual:
        raise

I couldn't find an equivalent debugpy.post_mortem() as there is for pdb and wdb.

How can I trigger a post-mortem debugging explicitly, similar to the "Uncaught Exceptions" checkbox, but for exceptions that are going to be caught on earlier frames?

LeoRochael
  • 14,191
  • 6
  • 32
  • 38
  • exceptionBreakpointFilters / setExceptionBreakpoints is VS Code may do what you want, see the discussion [here](https://github.com/microsoft/debugpy/issues/275) – stevecu Sep 04 '22 at 10:18

0 Answers0