try-except-else中的其他块

时间:2018-10-15 13:47:49

标签: python

Else:块的定义如下,如果在try块中没有异常,将执行语句。 因此,如果我们在try块中有异常,将执行except块,而不会执行try块的其余部分! 如果我们在try块中没有异常,则try块将执行,而except块将不执行!

try:
    print('hello')
except:
    print('hi')
else:
    print('bye')

---------------
try:
    print('hello')
    print('bye')
except:
    print('hi')

问题:

这两个代码有什么区别?

else块的任务是什么?为什么当它无效时我们使用它?

0 个答案:

没有答案