异常处理
可以不用写循环遍历判断的代码,如果出错,直接执行出错的语句,提高效率
检测异常:
try:
检测范围
except Exception [as reason]:
出现异常(Exception)后的代码
finally:
一定会执行的代码
try:
f = open('')
print(f.read())
except OSError as reason: /except (OSError, TypeError):
print('Error:' + reason)
except:
print('Unknow Error')
finally:
f.close()
引发异常
raise Excption(str)