python之处理异常

# 多分支报错

try:
    print("hello")
    # print(x)
    print("======>")

except KeyError as k:
    raise KeyError()
    # print(k)

except NameError as n:
    print(n)
else:
    print("try code 没有异常,就执行我")

finally:
    print("无论有无异常都执行,一般执行清理操作")



# 万能异常

try:
    print(x)
except Exception as e:
    print(e)
else:
    print("try code 没有异常,就执行我")
finally:
    print("无论有无异常都执行,一般执行清理操作")

 

posted @ 2017-05-02 16:30  Adamanter  阅读(72)  评论(0编辑  收藏  举报