Max*_*axS 1 python exception try-catch raise valueerror
我处理数据,对于某些示例,数据是有问题的。Python 提出了一个
ValueError:残差在初始点不是有限的。
是否有可能仅通过消息捕获值错误"Residuals are not finite in the initial point."?我试过:
try:
[code that could raise the error]
except Exception as e:
if e=='ValueError(\'Residuals are not finite in the initial point.\')':
[do stuff I want when the Residuals are not finite]
else:
raise e
Run Code Online (Sandbox Code Playgroud)
但它仍然一直引发错误。有没有办法实现我想象的样子?
谢谢
try:
[code that could raise the error]
except ValueError as e:
if len(e.args) > 0 and e.args[0] == 'Residuals are not finite in the initial point.':
[do stuff I want when the Residuals are not finite]
else:
raise e
Run Code Online (Sandbox Code Playgroud)
您可能必须检查是否e.args[0]完全包含该字符串(引发错误并打印e.args[0])