python的异常处理
- python使用try…except…,可使程序不因为运行错误而崩溃。结构如下:
try:
<body>
except <ErrorType1>:
<handler1>
except <ErrorType2>:
<handler2>
except:
<handler0>
- python的异常算是语句还可以使用else和finally关键字。
try:
<body>
except <ErrorType1>:
<handler1>
except <ErrorType2>:
<handler2>
except:
<handler0>
else:
<process_else>
finally:
<process_finally>
注:当try中的语句无异常,则执行else中的语句,finally中的语句无论有无异常,都会执行。