异常处理 try except else

 

 1、try ...except...else结构

                

'''
try ...except...else结构
如果try块中没有抛出异常,则执行else块,如果try中抛出异常,则执行except块
'''

try:
    a=int(input('请输入被除数:'))
    b=int(input('请输入除数:'))
    result=a/b
    print(result)
except BaseException as e:  #将错误命名为e
    print('出错了',e)
else:
    print('计算结果为:',result)

 2、try ...except...else...finally结构

'''
try ...except...else...finally结构
如果try块中没有抛出异常,则执行else块,如果try中抛出异常,则执行except块
'''

try:
    a=int(input('请输入被除数:'))
    b=int(input('请输入除数:'))
    result=a/b
    print(result)
except BaseException as e:
    print('出错了',e)
else:
    print('计算结果为:',result)
finally:                                   # 无论出现什么情况都运行该语句
    print('感谢您的应用')

3、常见异常

                                                                                      

原文地址:https://www.cnblogs.com/forhowcar/p/13900416.html