27

In PyCharm debugging mode, is there way to let it stop right after it hits an error but not exit and highlight the offending line? The analogous feature I have in mind is "dbstop if error" of Matlab.

11
  • I am not sure why one would need such a feature. The offending line is printed last in the traceback and quite easy to follow.
    – Ian
    Commented Oct 12, 2016 at 5:55
  • 8
    @ian_itor: Suppose the error first occurs on that offending line at one of n iterations. What do you suggest to locate that offending iteration, looping through all the prior correct iterations by hand?
    – Hans
    Commented Oct 12, 2016 at 8:23
  • 1
    Good point! I wasn't criticizing your cause, I was genuinely curious.
    – Ian
    Commented Oct 12, 2016 at 8:25
  • @ian_itor: I am glad you asked. It is a good question.
    – Hans
    Commented Oct 12, 2016 at 18:49
  • 1
    Possible duplicate of Break on unhandled exception in pycharm Commented Sep 1, 2017 at 6:54

2 Answers 2

31

Yes, there is. Under Run, if you hit View Breakpoints (Ctrl + Shift + F8 on Windows) there's a checkbox where you can create an exception breakpoint for any exception.enter image description here

10
  • 1
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review Commented Jan 14, 2018 at 0:43
  • 2
    Thanks for the clarification. If what the OP asks is not possible, do workarounds count as answers? Commented Jan 14, 2018 at 0:49
  • 4
    Yes, indeed it does. Thank you. I have accepted your answer and +1.
    – Hans
    Commented Oct 26, 2018 at 0:28
  • 2
    It also shows the location which is excellent because i was lost while searching it.
    – Alper
    Commented Mar 4, 2019 at 11:05
  • 2
    Great answer. Thanks.
    – Tom
    Commented Aug 31, 2019 at 10:44
2

The given answer was confusing to me. To do this you do not need to go to preferences, what you need to go is to break points menu bar (which is not in preferences for some reason). So:

  1. Go to break point by pressing Command + shift + fn + F8 (Ctrl + Shift + F8 on Windows) or go to the breakpoints you have at the bottom (see picture 1). This opens the break point menu (see picture 2).

  2. Then click enabled (for me suspend was already on so I only needed to click enabled). That's it.

Pic 1: enter image description here Pic 2: enter image description here


Note: the reason this feature is useful is because after running an execution it halts with exactly the program state it caused it to err. For me I have stochastic code due to machine learning and reproducing the error exactly is annoying. I'd rather just see what cased the error, inspect the program state, stack etc and just fix it. There is even a little window at the bottom right to run code so I can test it right there while I am fixing it. I can even do control+shift+E to test pieces of code from my actual code (as I right new code).

1
  • 2
    This does not work for me, PyCharm 2022.3.2 (Professional Edition). If I set to On raise it will raise any (also handled) exceptions, with on termination it does nothing...
    – meow
    Commented Feb 1, 2023 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.