2.9Pythony语言的错误与异常(Exception)

try/except

try:
    <可能会抛出异常的语句>
except [异常类型]:
    处理异常

try/except/except

try:
    <可能会抛出异常的语句>
except [异常类型1]:
    处理异常
except [异常类型2]:
    处理异常
...
except [异常类型n]:
    处理异常

try/except/finally

try:
    <可能会抛出异常的语句>
except [异常类型1]:
    处理异常
except [异常类型2]:
    处理异常
...
except [异常类型n]:
    处理异常
finally:
    <释放资源>

try/except/else/finally

try:
    <可能会抛出异常的语句>
except [异常类型1]:
    处理异常
except [异常类型2]:
    处理异常
...
except [异常类型n]:
    处理异常
else:
    <执行语句>
finally:
    <释放资源>

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/python/pythonbasic/5921.html

发表评论

登录后才能评论