异常处理的else和finally部分是否多余?例如,以下两个代码段之间是否有区别?
try:
foo = open("foo.txt")
except IOError:
print("error")
else:
print(foo.read())
finally:
print("finished")
和
try:
foo = open("foo.txt")
print(foo.read())
except IOError:
print("error")
print("finished")
更笼统地说,不能else总是将内容移动到try,并且不能finally仅将内容移动到try / catch块之外吗?如果是的话,elseand的目的是finally什么?只是为了提高可读性?