try:
np.arccos(10)
except RuntimeWarning:
print('error')enter code here
但没有任何反应,我是一个新人。
答案 0 :(得分:0)
如果你想引发警告引发异常,你可以使用warnings.simplefilter("error")
强制警告进入异常,例如:
In []:
import warnings
warnings.simplefilter("error")
try:
np.arccos(10)
except RuntimeWarning:
print('error')
Out[]:
error
或者,您可以使用warnings.catch_warnings(record=True)
上下文管理器记录警告:
In []:
import warnings
with warnings.catch_warnings(record=True) as ws:
np.arccos(10)
for w in ws:
print(w.message)
Out[]:
invalid value encountered in arccos