SwimBikeRun
SwimBikeRun

Reputation: 4460

How to skip over line throwing exception when debugging

# 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

Upvotes: 6

Views: 2229

Answers (2)

Hariom Singh
Hariom Singh

Reputation: 3632

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

Upvotes: 1

Milk
Milk

Reputation: 2655

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.

Upvotes: 3

Related Questions