在我的应用程序中,我正在从文件中加载一些数据。这可能会失败并引发异常。因此,我想记录一个警告并继续加载下一个数据。
我正在这样做:
try:
data_A = getDefaultConf(param_1,param_2)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
try:
data_B = getDefaultConf(param_1,param_3)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
try:
data_C = getDefaultConf(param_4,param_5)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
try:
data_D = getDefaultConf(param_4,param_6)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
try:
data_E = getDefaultConf(param_4,param_7)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
它有效,但看起来很重。所以,我的问题是:有没有办法让它更轻?它可能是这样工作的:
try:
data_A = getDefaultConf(param_1,param_2)
data_B = getDefaultConf(param_1,param_3)
data_C = getDefaultConf(param_4,param_5)
data_D = getDefaultConf(param_4,param_6)
data_E = getDefaultConf(param_4,param_7)
except Exception as e:
log(WARNING, "Failed to get default parametres: "+str(e))
# Here something that could work like a "continue" statement so that if loading of
# data_2 fails it will store the log and continue by trying to load data_3, etc.
我曾经在其他帖子中看到过一些建议将其放入循环中的答案,但是管理我的参数会不会更重?
我正在以这种方式等待任何建议。无论如何,我的代码都在工作。所以,这只是一个好奇的问题,它也可以帮助其他程序员......