异常处理的其他内容

try 代码块没有异常,会执行 else

finally 不管有无异常,都会执行

主动触发异常 raise TypeEroor()

自定义异常

断言 assert条件——》assert res1 = 1 ——》if res1 !=1  raise AssertionError

什么时候用异常处理:只有在有些异常无法预见的情况下,才应该加上try。。。except,其他的逻辑错误应该尽量修正。

猜你喜欢

转载自www.cnblogs.com/jintian/p/10959167.html