异常处理语法

抛出异常:

try:
  print("handel")
  #raise #抛出异常

except Exception:
  print('error!')
else:
  print("no error just exec!!")
finally:
  print("aways exec!")

  print("一般用来关闭文件,sock等")

 

print("---------------")

 

断言:抛出布尔型错误。

try:
  assert 1 == 0
except AssertionError:
  print("not equal")

 

posted @ 2016-09-16 14:52  xuanhui  阅读(167)  评论(0编辑  收藏  举报