0
try:
np.arccos(10)
except RuntimeWarning:
print('error')enter code here
mais rien ne se passe, je suis un plus récent.Comment obtenir les informations de RuntimeWarning en numpy?
try:
np.arccos(10)
except RuntimeWarning:
print('error')enter code here
mais rien ne se passe, je suis un plus récent.Comment obtenir les informations de RuntimeWarning en numpy?
Si vous voulez provoquer un avertissement à soulever une exception que vous pouvez utiliser warnings.simplefilter("error")
qui forcera l'avertissement à une exception, par exemple:
In []:
import warnings
warnings.simplefilter("error")
try:
np.arccos(10)
except RuntimeWarning:
print('error')
Out[]:
error
Alternative, vous pouvez enregistrer les avertissements avec le gestionnaire warnings.catch_warnings(record=True)
de contexte:
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