python異常處理

[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  


第一: 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" 

http://blog.csdn.net/spch2008/article/details/9343207

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章