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. – Ian Oct 12 '16 at 05:55
-
4@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 Oct 12 '16 at 08:23
-
1Good point! I wasn't criticizing your cause, I was genuinely curious. – Ian Oct 12 '16 at 08:25
-
@ian_itor: I am glad you asked. It is a good question. – Hans Oct 12 '16 at 18:49
-
" What do you suggest to locate that offending iteration " -- Have a look at conditional breakpoints: https://www.jetbrains.com/help/pycharm/2016.2/configuring-breakpoints.html -- This is if you know something about the context around your error. May not be of any use if it happens randomly. – Vlad B Oct 13 '16 at 08:27
-
@VladB: I know the conditional option of the breakpoint setting. However, as you mentioned, it only works if I know the offending condition. I do not think the word "randomly" is accurate since the error occurs deterministically reproducibly. It is only that I would not know in advance at which iteration and under what condition it occurs. – Hans Oct 13 '16 at 09:15
-
1Possible duplicate of [Break on unhandled exception in pycharm](https://stackoverflow.com/questions/5839286/break-on-unhandled-exception-in-pycharm) – Jean-François Corbett Sep 01 '17 at 06:54
-
@Jean-FrançoisCorbett: Have you read through the conversation before your comment? Does the linked question refer to breaking on any error which I need or breaking only on errors the type of which is specified in advance (in other words I have to know the context around my error) which I do not need? – Hans Sep 01 '17 at 07:36
-
@Hans Sorry, that comment was automatically generated, not written by me. I thought the other question addressed your issue, but I admit that it may not be the case. – Jean-François Corbett Sep 01 '17 at 09:18
-
@Jean-FrançoisCorbett: By "that comment" are you referring to the one in the question itself not your first comment in the comments below it? So can you take a careful look at the question, and comments below and the linked question and see if it does answer my question? – Hans Sep 01 '17 at 09:23
-
@ ["that comment".](https://stackoverflow.com/questions/39988590/stop-pycharm-if-error?noredirect=1#comment78949317_39988590) Apologies, no time for a careful look. Skimming [that other question](https://stackoverflow.com/questions/5839286/break-on-unhandled-exception-in-pycharm), it looked like what you're looking for, but I may be mistaken. Sorry for the negligence. – Jean-François Corbett Sep 01 '17 at 12:30
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.

- 3,189
- 4
- 26
- 34
-
1This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/18509517) – Thomas Smyth - Treliant Jan 14 '18 at 00:43
-
2Thanks for the clarification. If what the OP asks is not possible, do workarounds count as answers? – ThinkDigital Jan 14 '18 at 00:49
-
-
4
-
-
2It also shows the location which is excellent because i was lost while searching it. – Alper Mar 04 '19 at 11:05
-
2
-
-
It is not stopping on `RuntimeError`. Any idea how to catch it? – Nagabhushan S N Sep 28 '21 at 11:36
-
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).

- 5,884
- 57
- 198
- 323
-
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 Feb 01 '23 at 17:28