1

I have a function f that is called several times in my code. The details of this function are not important, other than that it may at times generate a RuntimeWarning because an iteration within the function is not making progress. When this happens I wish to print a message to the user alerting them which input resulted in the function raising the warning. Example:

for x in numpy.arange(5):
    y = f(x)
    #if calculation of y raised a RuntimeWarning then print "x = %.2f is problematic" % x

Any solutions?

EDIT:

I have seen the question that is referenced as the reason for this post being a duplicate, but I feel that my question is different. I simply want to print a message if a warning was generated for a particular function call... Not raise any exceptions.

Dipole
  • 1,840
  • 2
  • 24
  • 35
  • Maybe [this](https://stackoverflow.com/questions/15933741/how-do-i-catch-a-warning-in-python-like-its-an-exception-not-just-for-testing) would interest you? – Alyssa Haroldsen Jul 27 '15 at 21:40

0 Answers0