重新引发异常的目的是什么?
问题内容:
因此,我在其他地方看到过提到使用以下内容重新引发异常。
try:
whatever()
except:
raise
重新引发异常的目的是什么?当然,一个未捕获的异常会上升到最高峰吗?即:
try:
int("bad")
except:
raise
具有与以下相同的输出:
int("bad")
即我在控制台中出现ValueError。
问题答案:
想象下面的代码。
进行一些设置:例如,您负责维护一个巨大的信息数据库,任何数据丢失将是灾难性的!
huge_dictionary = {'lots_of_important':['stuffs']}
try:
check_data(new_data) #make sure the data is in the correct format
huge_dictionary['lots_of_important'].append(new_data)
except:
data_writer.backup(huge_dictionary)
data_writer.close()
#and any other last second changes
raise