python异常处理
python异常处理使用try,catche,else和finally关键字来尝试可能未成功的操作,处理失败及正常情况以及在事后清理资源
try:
block_try
except Exception 1: 、、第一种except形式
block_when_exceptin_1_happen:
except (Exception_1 Exception_2 Exception_3): //第二种except形式
block_when_exception_1_or_2_or_3_happen
except Exception_5 variance:
block_when_exception_5_happen
except (Exception_6 Exception_7),variance:
block_when_exception_6_or_7_happen
except :
block_for_all_other_exceptions
else:
block_for_no_exception
finally:
block_anyway
自定义异常
自定义异常的边长方法是建立一个继承子系统异常的子类,并且需要引发该异常时用raise语句跑出异常