開發(fā)人員在編寫程序時(shí),難免會(huì)遇到錯(cuò)誤,有的是編寫人員疏忽造成的語法錯(cuò)誤,有的是程序內(nèi)部隱含邏輯問題造成的數(shù)據(jù)錯(cuò)誤,還有的是程序運(yùn)行時(shí)與系統(tǒng)的規(guī)則沖突造成的系統(tǒng)錯(cuò)誤,等等。
總的來說,編寫程序時(shí)遇到的錯(cuò)誤可大致分為 2 類,分別為語法錯(cuò)誤和運(yùn)行時(shí)錯(cuò)誤。
語法錯(cuò)誤,也就是解析代碼時(shí)出現(xiàn)的錯(cuò)誤。當(dāng)代碼不符合 Python 語法規(guī)則時(shí),Python解釋器在解析時(shí)就會(huì)報(bào)出 SyntaxError 語法錯(cuò)誤,與此同時(shí)還會(huì)明確指出最早探測到錯(cuò)誤的語句。例如:
print "Hello,World!"
我們知道,Python 3 已不再支持上面這種寫法,所以在運(yùn)行時(shí),解釋器會(huì)報(bào)如下錯(cuò)誤:
SyntaxError: Missing parentheses in call to 'print'
語法錯(cuò)誤多是開發(fā)者疏忽導(dǎo)致的,屬于真正意義上的錯(cuò)誤,是解釋器無法容忍的,因此,只有將程序中的所有語法錯(cuò)誤全部糾正,程序才能執(zhí)行。
Python運(yùn)行時(shí)錯(cuò)誤
運(yùn)行時(shí)錯(cuò)誤,即程序在語法上都是正確的,但在運(yùn)行時(shí)發(fā)生了錯(cuò)誤。例如:
a = 1/0
上面這句代碼的意思是“用 1 除以 0,并賦值給 a 。因?yàn)?0 作除數(shù)是沒有意義的,所以運(yùn)行后會(huì)產(chǎn)生如下錯(cuò)誤:
>>> a = 1/0
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a = 1/0
ZeroDivisionError: division by zero
以上運(yùn)行輸出結(jié)果中,前兩段指明了錯(cuò)誤的位置,最后一句表示出錯(cuò)的類型。在 Python 中,把這種運(yùn)行時(shí)產(chǎn)生錯(cuò)誤的情況叫做異常(Exceptions)。這種異常情況還有很多,常見的幾種異常情況如表 1 所示。
表 1 Python常見異常類型
異常類型 |
含義 |
實(shí)例 |
AssertionError |
當(dāng) assert 關(guān)鍵字后的條件為假時(shí),程序運(yùn)行會(huì)停止并拋出 AssertionError 異常 |
>>> demo_list = ['C語言中文網(wǎng)']
>>> assert len(demo_list) > 0
>>> demo_list.pop()
'C語言中文網(wǎng)'
>>> assert len(demo_list) > 0
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
assert len(demo_list) > 0
AssertionError |
AttributeError |
當(dāng)試圖訪問的對(duì)象屬性不存在時(shí)拋出的異常 |
>>> demo_list = ['C語言中文網(wǎng)']
>>> demo_list.len
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
demo_list.len
AttributeError: 'list' object has no attribute 'len' |
IndexError |
索引超出序列范圍會(huì)引發(fā)此異常 |
>>> demo_list = ['C語言中文網(wǎng)']
>>> demo_list[3]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
demo_list[3]
IndexError: list index out of range |
KeyError |
字典中查找一個(gè)不存在的關(guān)鍵字時(shí)引發(fā)此異常 |
>>> demo_dict={'C語言中文網(wǎng)':"c."}
>>> demo_dict["C語言"]
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
demo_dict["C語言"]
KeyError: 'C語言' |
NameError |
嘗試訪問一個(gè)未聲明的變量時(shí),引發(fā)此異常 |
>>> C語言中文網(wǎng)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
C語言中文網(wǎng)
NameError: name 'C語言中文網(wǎng)' is not defined |
TypeError |
不同類型數(shù)據(jù)之間的無效操作 |
>>> 1+'C語言中文網(wǎng)'
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
1+'C語言中文網(wǎng)'
TypeError: unsupported operand type(s) for +: 'int' and 'str' |
ZeroDivisionError |
除法運(yùn)算中除數(shù)為 0 引發(fā)此異常 |
>>> a = 1/0
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a = 1/0
ZeroDivisionError: division by zero |
提示:表中的異常類型不需要記住,只需簡單了解即可。
當(dāng)一個(gè)程序發(fā)生異常時(shí),代表該程序在執(zhí)行時(shí)出現(xiàn)了非正常的情況,無法再執(zhí)行下去。默認(rèn)情況下,程序是要終止的。如果要避免程序退出,可以使用捕獲異常的方式獲取這個(gè)異常的名稱,再通過其他的邏輯代碼讓程序繼續(xù)運(yùn)行,這種根據(jù)異常做出的邏輯處理叫作異常處理。
開發(fā)者可以使用異常處理全面地控制自己的程序。異常處理不僅僅能夠管理正常的流程運(yùn)行,還能夠在程序出錯(cuò)時(shí)對(duì)程序進(jìn)行必的處理。大大提高了程序的健壯性和人機(jī)交互的友好性。
那么,應(yīng)該如何捕獲和處理異常呢?可以使用 try 語句來實(shí)現(xiàn)。有關(guān) try 語句的語法和用法,會(huì)在后續(xù)章節(jié)繼續(xù)詳解。
|