Why is else clause needed for try statement in python ? 尝试...除了......还有v嵌套的尝试......除了
把它推向深入:
try:
f = open('foo', 'r')
except IOError as e:
error_log.write('Unable to open foo : %s\n' % e)
else:
data = f.read()
f.close()
它的角落情况下,通过else clause
解决仍然能够避免发生在我由nested try...except
避免需要else
? :
try:
f = open('foo', 'r')
try:
data = f.read()
f.close()
except:
pass
except IOError as e:
error_log.write('Unable to open foo : %s\n' % e)
并且不能让代码工作。我得到无效的语法。 –
@eryksun:啊,是的,忘记了。我刚刚检查了Python文档和[With'语句](http://docs.python.org/reference/compound_stmts.html#with)只是说“版本2.5中的新功能”。而没有提到需要'__future__'导入,所以我想到'除了'''。 Python文档中的“复合语句”页面没有提及它何时被添加......并且我不记得。 –
很高兴知道谢谢 –