Bootstrap

python中异常处理

异常处理

# try except 结构
# coding: utf-8  
try:  
    num = int(input("intput score"))  
    if num < 100:  
        print("yes")  
except Exception as e:  
    print(e)  
    print("输入不合法")
# try except else结构
# coding: utf-8    
mathScore = input("数学分数")  
try:  
    mathScore = int(mathScore)  
except Exception as e:  
    print("输入数值有误")  
else:  
    print("输入数值合法")
# try except finally
# coding: utf-8  
  
chineseScore = input("中文成绩")  
try:  
    div = chineseScore / 0  
    print(div)  
except Exception as e:  
    print("出现异常")  
finally:  
    print("功能执行错误")
;