异常处理语法

抛出异常:

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")

原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5876448.html