这是一个在Django环境中运行的python脚本。我需要创建一个捕获'其余的错误',并提出一个例外,以便芹菜将发送一个电子邮件的例外。在
这是最好的办法吗?在
for thread in thread_batch:
try:
obj = query_set.get(thread_id=thread['thread_id'])
for key, value in thread.iteritems():
setattr(obj, key, value)
obj.unanswered = True
except ThreadVault.DoesNotExist:
obj = ThreadVault(**thread)
except:
raise Exception("There has been a unknown error in the database")
obj.save()
是的,空的
except
将捕捉与ThreadVault.DoesNotExist
(在本例中)不同的任何异常。但您可以进一步改进代码。在总是尽量在
try
块中放入尽可能少的代码。你的代码可以是:相关问题 更多 >
编程相关推荐