Catching Numpy All-Nan Slice

Viewed 10595

I have an issue trying to catch a Python exception:

File "/usr/lib/python2.7/dist-packages/numpy/lib/nanfunctions.py", line 427, in nanargmax raise ValueError("All-NaN slice encountered") ValueError: All-NaN slice encountered

The error appears with this code when effectively the slice contains All-NaN. However, I want to catch that situation and handle it.

with warnings.catch_warnings():
   warnings.filterwarnings('error')
try:
    action = np.nanargmax(self.Q[state])
except Warning as e:
    print "error"
    sys.exit(0)

I expect to print the word error, however, the try-except statement is ignored. Any help, please?

1 Answers
Related