Python开发-异常处理
程序员文章站
2022-07-05 18:18:54
...
文章目录
Python异常处理方式:
- try/exception结构
try:
pass
except IOError:
pass
- try/exception…else
说明:else子句在try未发生异常情况下执行;
try:
pass
except IOError:
pass
else:
pass
- try/exception…finally
说明:finally部分无论try是否发生异常,都会执行;
try:
pass
except IOError:
pass
finally:
pass
- try/exception…else…finally
说明:
1)finally部分无论try是否发生异常,都会执行;
2)一个异常如果在exception里没有被捕获,那么会在finally子句执行后抛出;
try:
pass
except IOError:
pass
else:
pass
finally:
pass
- 多个exception捕获
try:
pass
except OSError as err:
pass
except ValueError:
pass
except:
pass
- 多个exception处理
说明:exception里可以同时包含多个异常,放到一个元祖里;
try:
pass
except (OSError,ValueError,RuntimeError):
pass
except:
pass
- raise异常抛出
raise exception(,args)
上一篇: Spring声明式事务