python异常处理代码执行说明

python异常处理代码执行说明

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#This is note foe exception

try:
  code    #需要判断是否会抛出异常的代码,如果没有异常处理,python会直接停止执行程序

except:  #这里会捕捉到上面代码中的异常,并根据异常抛出异常处理信息
#except ExceptionName,args:    #同时也可以接受异常名称和参数,针对不同形式的异常做处理

  code  #这里执行异常处理的相关代码,打印输出等


else:  #如果没有异常则执行else

  code  #try部分被正常执行后执行的代码

finally:
  code  #退出try语句块总会执行的程序



#函数中做异常检测
def try_exception(num):
  try:
    return int(num)
  except ValueError,arg:
    print arg,"is not a number"
  else:
    print "this is a number inputs"


try_exception('xxx')

#输出异常值
Invalide literal for int() with base 10: 'xxx' is not a number

注意事项

不要在 try 里写返回值。 try-except-else 里都是指做某事,而不是处理返回。如果在 try 里面写返回值,则 else 部分是 unreachable 的。

def try_exception(num): try: return int(num) except ValueError,arg: print arg,"is not a number" else: print "this is a number inputs"

猜你喜欢

转载自www.cnblogs.com/ngbjng/p/12015055.html