try中else问题
书上有段代码程序代码:
try: x=1/0#line27 except NameError as e: print('wrong:', e) else: print('That went well,no problem') finally: print('Cleaning up...')
结果:
Cleaning up...
Traceback (most recent call last):
File "D:\code\aptana studio3\temp\temp.py", line 27, in <module>
x=1/0
ZeroDivisionError: division by zero
把NameError改成Exception就没有问题。
如果except捕获不到错误就必须停止,那要else有什么用?
直接改成
程序代码:
try: x=1/0#line27 print('That went well,no problem') except NameError as e: print('wrong:', e) finally: print('Cleaning up...')不也是一样的吗?
[此贴子已经被作者于2017-9-6 23:20编辑过]