當我們認為某些**可能會出錯時,就可以用try來執行這段**,如果執行出錯,則後續**不會繼續執行,而是直接跳轉至錯誤處理**,即except語句塊,執行完except後,如果有finally語句塊,則執行finally語句塊,至此,執行完畢。
例:>>> a = b
traceback (most recent call last):
file "", line 1, in
nameerror: name 'b' is not defined
>>> try :
... a = b
... except nameerror,e:
... print e
...
name 'b' is not defined
>>> try :
... a = b
... except nameerror,e:
... print e
... finally:
... print "finally"
...
name 'b' is not defined
finally
>>>
Python編碼錯誤處理
在將字串寫入檔案時,執行f.write str 後台總是報錯 unicodeencodeerror ascii codec can t encode character u u6211 in position 0 ordinal not in range 128 即ascii碼無法被轉換成unico...
python 錯誤處理 assert
assert是斷言的意思,解釋為 我斷定這個程式執行之後或者之前會有這樣的結果,如果不是,那就扔出乙個錯誤。語法 assert expression arguments assert 表示式 引數 舉例 def foo s n int s assert n 0,n is zero return 10...
python 錯誤處理 assert
assert是斷言的意思,解釋為 我斷定這個程式執行之後或者之前會有這樣的結果,如果不是,那就扔出乙個錯誤。語法 assert expression arguments assert 表示式 引數 舉例 def foo s n int s assert n 0,n is zero return 10...