user3246971
user3246971

Reputation: 243

Debugging Advice for Python

I have to debug large programs with nested function calls. I would like it s.t. whenever an exception occurs I simply halt the execution at that point, within any function I might be in. Then I can try out different corrections for the error and move on.

While I have been using try except, what I need here is that I can halt inside any function, no matter how nested the call to it might be. So, to do it manually I would have to wrap each function's code around a try-except! like so:

def fun1:
  try:

  except:
    pdb.set_trace()

And this would be very cumbersome to write. Also since whenever I encounter an exception I go straight to the except block, for large functions this would require me to restart from the beginning, which would be time taking. So basically I have this (ambitious) requirement of running each line of code in it's own try-except block, like:

def func1:
  try:
    line1
  except:
    pdb.set_trace()

  try:
    line2
  except:
    pdb.set_trace()

Is there some automatic, or clever way to rig up such a system? Thanks in advance.

Upvotes: 0

Views: 36

Answers (1)

Joel
Joel

Reputation: 2257

Any good IDE will have the ability to add breakpoints to debug your code. I personally use PyCharm by Jetbrains jetbrains.com/pycharm. You can add breakpoints and step line-by-line through your code easily. It also automatically halts execution at an exception and you can manipulate values. How are you developing your python code now?

Upvotes: 1

Related Questions