28

I have encountered "RuntimeWarning: overflow encountered in exp ..." in my code. How do I make pyCharm break on this warning? It currently runs past it.

1

3 Answers 3

34

I did my own research and similarly to what @doctorlove said, do

numpy.seterr(all='raise')

then numpy will raise exceptions instead of RuntimeWarnings. The exceptions can then be caught by PyCharm.

Sign up to request clarification or add additional context in comments.

3 Comments

I take it you didn't find a better solution to this problem? Because breakpoints stop the application, where as I would like to have it break but able to continue...
@shadow The exception can be caught manually since you can localize it in your code using the debugger.
for those using scipy, there is a scipy equivalent to numpy.seterr which is scipy.special.seterr
11

You can use warning module to turn the warning into an error, then try-except and set break point:

import warnings
warnings.simplefilter('error')
try:
   #code that generates warning
except:
   #put a breakpoint here

2 Comments

How do you remove it later? I want to catch warnings only in a specific block. I wish to add something like warnings.simplefilter('') after the except section
for me (in PyCharm), this does not show the stack so is not very useful: there is indeed a pause at breakpoint, but with no added value.
0

A modification to Shuo Wang's answer:

import warnings
warnings.simplefilter('error')
try:
   #code that generates warning
except:
    #put a breakpoint here
    pass
pass

Put the breakpoint on the pass statement that is inside the except clause. After you've reached that pass, step over it to land on the pass (which is optional, btw) that's outside the try-except block. This should allow you to see the call stack and all the variable values.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.