python极简笔记——异常

#异常
def exceptionFunc():
  print(7/0)
#exceptionFunc()

#使用try-except 处理异常
def sloveExecption():
  try:
    print(7/0)
  except ZeroDivisionError:
    print('divide by zero!!!!')
sloveExecption()

#else代码块
#依赖于 try 代码块成功执行的代码都应放到 else 代码块中
def elseDemo():
  while True:
    first = input('input first number:')
    second = input('input second number:')
    if(first == 'q'):
      break
    try:
      res = int(first) / int(second)
    # Exception是任何运行时异常的父异常
    except Exception:
      print('divide by zero!!')
    else:
      print(res)
elseDemo()

#FileNotFoundError 文件找不到异常


猜你喜欢

转载自blog.csdn.net/sinat_22808389/article/details/94718770