python 异常

2020-01-10 01:22:03 浏览数 (2)

最长用到的异常处理的形式:

try:

    正常的操作

except:

    发生异常,执行这块代码

else:

    如果没有异常执行这块代码

finally:

    不管如何,最后一定要执行的代码

try:

    10/0

except Exception as e:

    print ('出错了:', e)

else:

    print('ok')

finally:

    print('finally')

出错了: division by zero

finally

try:

    10/5

except Exception as e:

    print ('出错了:', e)

else:

    print('ok')

finally:

    print('finally')

ok

finally

0 人点赞