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?
Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.
After you have found an exception, or preferably before your software is distributed, you should go through the code and debug it in order to find and repair the erroneous code. There are many different ways to debug and repair code; we will go through some debugging methodologies in this chapter.
If a user-mode debugger is attached, the program will break into the debugger. This means that the program will pause and the debugger will become active.
You don't want to break on every exception; idiomatic Python code uses exceptions heavily (EAFP) so you'd be continually breaking in unrelated code.
Instead, use pdb post-mortem: import pdb; pdb.pm()
. This uses sys.last_traceback
to inspect the stack including the locals at the throw point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With