重新提出异议的要点是什么? [英] What is the point of re-raising exceptions?

查看:171
本文介绍了重新提出异议的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在其他地方看到使用以下内容来重新提出异常。

So I've seen mention elsewhere of using the following to re-raise an exception.

try:
    whatever()
except:
    raise

引发例外?当然一个未被捕获的例外只会提升到顶部?即:

What is the purpose re-raising an exception? Surely an uncaught exception will just raise to the top anyway? i.e:

try:
    int("bad")
except:
    raise

具有相同的输出:

int("bad")

ie我在控制台中收到一个ValueError。

i.e. I get a ValueError in the console.

推荐答案

想象下面的代码。

一个设置:你有责任维护一个巨大的信息数据库,例如数据丢失将是灾难性的!

A little setup: You are responsible for maintaining a huge database of information for example, and any loss of data would be catastrophic!

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

这篇关于重新提出异议的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆