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.
-
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.– IanCommented 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?– HansCommented Oct 12, 2016 at 8:23
-
1Good point! I wasn't criticizing your cause, I was genuinely curious.– IanCommented Oct 12, 2016 at 8:25
-
@ian_itor: I am glad you asked. It is a good question.– HansCommented Oct 12, 2016 at 18:49
-
1Possible duplicate of Break on unhandled exception in pycharm– Jean-François CorbettCommented Sep 1, 2017 at 6:54
2 Answers
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.
-
1This 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
-
2Thanks for the clarification. If what the OP asks is not possible, do workarounds count as answers? Commented Jan 14, 2018 at 0:49
-
4Yes, indeed it does. Thank you. I have accepted your answer and +1.– HansCommented Oct 26, 2018 at 0:28
-
2It also shows the location which is excellent because i was lost while searching it.– AlperCommented Mar 4, 2019 at 11:05
-
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:
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).Then click enabled (for me suspend was already on so I only needed to click enabled). That's it.
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).
-
2This 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...– meowCommented Feb 1, 2023 at 17:28