【发布时间】:2013-08-11 02:31:32
【问题描述】:
我想在网络中存储一些数据。我想做两个操作:第一个是打开一个URL,第二个是存储数据,它们都在try...except块中。
我想知道嵌套try...except 好不好,为什么。
解决方案一:
try:
# open url
# store data
except:
# url doesn't exist
# cannot store
解决方案二:
try:
# open url
try:
# store data
except:
# cannot store
except:
# cannot open url
【问题讨论】:
-
第一次和第二次操作的错误处理有什么区别吗?
-
还要考虑
try..except..else[1] 是否合适,以免意外捕获您无意捕获的异常。 [1]docs.python.org/2/tutorial/errors.html -
你可以看看this thread。
-
@dbf thegrinner 帮我修改了代码,所以它们现在不同了