20309 发表于 2018-8-14 11:20:47

python3学习之异常

  
##异常处理
  
#try:
  
#    pass
  
#except Exception as ex:   Exception(捕获所有错误)可以换成任何异常类型,代表只捕获指定错误,
  
# 可以写多个except
  
#    pass
  
##异常类型:
  
#
  
#
  
##完整的异常代码:
  
# try:
  
#    raiseException('testeetst')#主动触发异常 ,Exception可以是其他异常类型
  
# except ValueError as ex:
  
#   print(ex)
  
# except Exception as ex:
  
#   pass
  
# else:
  
#   pass
  
# finally:
  
#   pass
  

  
##自定义异常
  
#继承Exception类
  
#
  
##断言
  
#assert 1==1相当于if else然后抛出异常
  

  

  
while True:
  
    n1 = input("num1: ")
  
    n2 = input("num2: ")
  
    try:
  
      n1 = int(n1)
  
      n2 = int(n2)
  
      print(n1 + n2)
  
    except Exception as e:
  
      print(e)
页: [1]
查看完整版本: python3学习之异常