python异常捕获try except

python的异常处理机制设计的比较传统,在日常的开发中,基本满足我的需要 

下面就python的异常,几点小小的讨论 

tommy@lab3:~$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
>>> try:
... 1/0
... except:
... print "do something..."
...
do something...
>>>

2. try...finally 

finally 里面只是执行完成try中的代码后,必须执行的代码, 
即使是 try中有异常抛出,也是会去执行finally 

>>> try:
... 1/0
... finally:
... print "I just finally do something ,eg: clear!"
...
I just finally do something ,eg: clear!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>




转载于:https://www.cnblogs.com/wuxi/archive/2011/12/11/python.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值