try:
print('try...')
r = 10 / 0
print('result:', r)
except BaseException as e:
print('except:',e)
finally: ##可以添加,或者删除,都会执行。
print('finally...')
print('END')
此外,如果没有错误发生,可以在except语句块后面加一个else,当没有错误发生时,会自动执行else语句:
try: print('try...') r = 10 / int('2') print('result:', r)except ValueError as e: print('ValueError:', e)except ZeroDivisionError as e: print('ZeroDivisionError:', e)else: print('no error!')finally: print('finally...')print('END')