How to skip over line throwing exception when debugging?
Coming from a Visual Studio debugging background, I'm finding the pycharm debugger to be missing two critical features:
- Break before exception
- Jump to line (similar to jump command in pdb)
As the 2nd will soon be added (yaaaay!), I want to ask about the 1st.
Currently when running in the debugger, once the exception is hit, that's it! No more walking through the code! Python exits the stack and the stack trace is shown. This is nonideal in that I have to start up the script again to get back to the context of where the error occurs. It would be much better to fix the error inline, or skip the offending line to check more code down the road, etc.
Consider the following example:
- Run the code to the context of bug - 10min
- The print throws an exception
- Add breakpoint at print
- Run again to context of bug (this example has been simplified to a simple exception, from something quite complicated with a large context of data and objects) - 10min
- Introspect and fix error
- Since there is no way to bypass error, once again re-run code - 10min
- Repeat for each additional unexpected exception found
# process_with_huge_time_overhead() # 10 minute overhead
list_a = [1,2,3]
print(list_a[3]) # Throws exception
# process2_with_huge_time_overhead()
new_data = [5,6,7]
list_a += new_data
print(list_a)
As seen, not having this feature to bypass exceptions when they unexpectedly occur adds an insane overhead.
Overhead with feature: 10min + time to introspect and fix further unexpected exceptions.
Overhead without feature: (30min + time to introspect and fix one exception ) * number of exceptions
Not only is the current workflow worse in flat overhead but it also scales with how many exceptions that will be hit. It's really painful!
Half jokingly, it could be as simple as piping myscript.py line by line into the basic python interpreter. When the python shell hits an exception, it doesn't lose the context, it gives a another chance to do something. That's the desired behavior.
Please sign in to leave a comment.
Hi Maxwell! You can enable On raise and Ignore library files options in Run | View Breakpoints | Python Exception Breakpoint | Any Exception, so debugger will stop right before exception is raised. This should partially solve your problem if I correctly understood your usecase. Unfortunately bypassing exceptions is not supported - please feel free to create a feature request in out bug tracker - https://youtrack.jetbrains.com/issues?q=project:%20PyCharm
Hi Pavel, That is pretty useful to break on any exception. Unfortunately the problem is the exception is already raised and the program will inevitably exit. The ideal behavior would be to alert that the exception has occurred on the line but not exiting the python program and taking the memory context with it.
The python interpreter (and jupyter notebook on a per cell basis) work this way by default. An exception does not take down all the work loaded into memory prior.
It would be AWESOME if this could be added.
A lot of modern python is juptyer notebook, but there still exist lots of 1000+ line single .py files where having a more flexible debugger would be insanely valuable, the number one reason being avoiding repeated overhead as stated above.
I just created a feature request for this: https://youtrack.jetbrains.com/issue/PY-30705