How to skip over line throwing exception when debugging
Asked Answered
G

2

6
# process_with_huge_time_overhead()
list_a = [1,2,3]
print(list_a[3])
# process_with_huge_time_overhead()
new_data = [5,6,7]
list_a += new_data

After reaching this line in ipdb (invoked via python -m ipdb script.py), the exception is thrown: IndexError enter image description here

How can one continue to debug and jump around without going through the overhead of getting to this point again?

If I jump to line 62 and use the n command to execute the next line, it doesn't work. every n just continues to exit the program.

enter image description here

Gullah answered 18/8, 2017 at 0:51 Comment(2)
I would say better add code there for exception ..if exception then continueCookbook
try: do_something() except Exception: passCookbook
O
3

You cannot do this without changing the program.

The debugger follows the code execution. If an error is thrown, the debugger will continue following the programs flow of error handling. If the error is not handled by you, a crash is issued. This is expected behavior and the debugger will follow it.

Omentum answered 18/8, 2017 at 1:4 Comment(1)
Yeah it's like I want to pipe everything line by line in the script.py to a python shell... The python shell is able to run code which throws an exception without shutting down and losing the current variables and context. This is the behavior I want! Coming from a visual studio C# debugging environment, I find it unbelievable python doesn't have this functionality, especially as a scripting language.Gullah
C
1

You can do something like this

try: 
    list_a = [1,2,3]
except Exception:
    pass
print(list_a[3])
# process_with_huge_time_overhead()
new_data = [5,6,7]
list_a += new_data

Why we cannot skip using pdb.

Yes we can by changing the stack frame data

pdb inspects arbitrary Python code in the context of any stack frame.so one way to skip that is change the stack frame data (indirectly you will change the whole logic) .Best is to handle the code exception

Since you are using pycharm you can set the value or in pdb you change the value.But thats not recommended (as that changes the logic)

Set value in debugger

Cookbook answered 18/8, 2017 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.