0

The following code produces a runtime warning because 1.0 is outside the range of the arctanh function, but the code continues running and prints inf for the result of the calculation, and then Not dead yet!

import numpy
from numpy import arctanh
print(arctanh(1.0))
print("Not dead yet!")

This behavior of numpy's arctanh function seems inconsistent with the way python normally handles arithmetic exceptions. E.g., if I change the arctanh to a 1.0/0.0, I get ZeroDivisionError, which causes the program to terminate immediately.

Is there some way to change the behavior of numpy so that this type of warning becomes an error, or so that the warning causes the program to terminate, as an error would?

  • 1
    Are you sure you understand what you're asking for? You say you *don't* want to have to wrap every calculation in a try/catch, but try/catch doesn't interact with warnings at all; wrapping every calculation in a try/catch would be pointless. If you configure NumPy to throw an error instead of raising a warning for this, *then* try/catch would do something (but you still wouldn't need to introduce try/catches unless you actually wanted to catch the exceptions). – user2357112 Jun 01 '18 at 19:32
  • @user2357112: Thanks for the clarification. So try/catch wouldn't even help. What I want is for the program to crash in this situation. –  Jun 01 '18 at 19:34
  • 4
    This answer might be helpful, [How do I catch a numpy warning like it's an exception](https://stackoverflow.com/questions/15933741/how-do-i-catch-a-numpy-warning-like-its-an-exception-not-just-for-testing) – vielkind Jun 01 '18 at 19:34
  • 2
    According to @vealkind's linked answer, `numpy.seterr(all='raise')` should do it. – Linuxios Jun 01 '18 at 19:36
  • Perfect, that's what I needed. Maybe my question should actually be closed as a duplicate. –  Jun 01 '18 at 19:38

0 Answers0