python 异常: try, expect, finally

稍微总结一下,否则总是忘。

[python] view plaincopyprint?

1. x = 'abc'  

2. def fetcher(obj, index):  

3.     return obj[index]  

4.   

5. fetcher(x, 4)  

输出:

[plain] view plaincopyprint?

1.   File "test.py", line 6, in <module>  

2.     fetcher(x, 4)  

3.   File "test.py", line 4, in fetcher  

4.     return obj[index]  

5. IndexError: string index out of range  

捕获列表列出的异常,进行处理。若except后无任何参数,则捕获所有异常。

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except:  

5.         print "got exception"                  


第一: try不仅捕获异常,而且会恢复执行

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except:  

5.         print "got exception"  

6.     print "continuing"  

输出:

[plain] view plaincopyprint?

1. got exception  

2. continuing  


第二:无论try是否发生异常,finally总会执行

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     finally:  

5.         print 'after fecth'  

输出:

[plain] view plaincopyprint?

1. after fecth  

2. Traceback (most recent call last):  

3.   File "test.py", line 55, in <module>  

4.     catcher()  

5.   File "test.py", line 12, in catcher  

6.     fetcher(x, 4)  

7.   File "test.py", line 4, in fetcher  

8.     return obj[index]  

9. IndexError: string index out of range  


第三:try无异常,才会执行else

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except:  

5.         print "got exception"  

6.     else:  

7.         print "not exception"  

输出:

[plain] view plaincopyprint?

1. got exception  

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 2)  

4.     except:  

5.         print "got exception"  

6.     else:  

7.         print "not exception"  

输出:

[plain] view plaincopyprint?

1. not exception  

else作用:没有else语句,当执行完try语句后,无法知道是没有发生异常,还是发生了异常并被处理过了。通过else可以清楚的区分开。

第四:利用raise传递异常

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except:  

5.         print "got exception"  

6.         raise  

输出:

[plain] view plaincopyprint?

1. got exception  

2. Traceback (most recent call last):  

3.   File "test.py", line 37, in <module>  

4.     catcher()  

5.   File "test.py", line 22, in catcher  

6.     fetcher(x, 4)  

7.   File "test.py", line 4, in fetcher  

8.     return obj[index]  

9. IndexError: string index out of range  

raise语句不包括异常名称或额外资料时,会重新引发当前异常。如果希望捕获处理一个异常,而又不希望

异常在程序代码中消失,可以通过raise重新引发该异常。

 

第五:except(name1, name2)

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except(TypeError, IndexError):  

5.         print "got exception"  

6.     else:  

7.         print "not exception"  

捕获列表列出的异常,进行处理。若except后无任何参数,则捕获所有异常。

[python] view plaincopyprint?

1. def catcher():  

2.     try:  

3.         fetcher(x, 4)  

4.     except:  

5.         print "got exception"  

 

第六:异常和sys模块

 另一种获取异常信息的途径是通过sys模块中的exc_info()函数。该函数回返回一个三元组:(异常类,异常类的实例,跟中记录对象)

[python] view plaincopy

1. >>> try:  

2. ...     1/0  

3. ... except:  

4. ...     import sys  

5. ...     tuple = sys.exc_info()  

6. ...   

7. >>> print tuple  

8. (<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x7f538a318b48>)  

9. >>> for i in tuple:  

10....     print i  

11....   

12.<type 'exceptions.ZeroDivisionError'#异常类      

13.integer division or modulo by zero #异常类的实例  

14.<traceback object at 0x7f538a318b48> #跟踪记录对象  

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值