Python 程序中最常見的錯誤原因是某個語句不符合規定的用法。這種錯誤稱為語法錯誤。Python 解釋器會立即報告它,通常會附上原因。
Example: Error
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
在 Python 3.x 中,print 是一個內置函數,需要括號。上面的語句違反了這種用法,因此會顯示語法錯誤。
但是很多時候,程序在運行后會導致錯誤,即使它沒有任何語法錯誤。這種錯誤是運行時錯誤,稱為異常。Python 庫中定義了許多內置的異常。讓我們看看一些常見的錯誤類型。
下表列出了 Python 中重要的內置異常。
例外 | 描述 |
---|---|
斷言錯誤 | assert 語句失敗時引發。 |
屬性錯誤 | 對屬性賦值或引用引發的。 |
歐費羅 | 當 input()函數達到文件結束條件時引發。 |
浮動指針錯誤 | 浮點運算失敗時引發。 |
GeneratorExit | 調用生成器的 close()方法時引發。 |
導入錯誤 | 找不到導入的模塊時引發。 |
索引錯誤 | 當序列的索引超出范圍時引發。 |
鍵錯誤 | 在字典中找不到鍵時引發。 |
鍵盤中斷 | 當用戶點擊中斷鍵(Ctrl+c 或 delete)時引發。 |
存儲器錯誤 | 當操作耗盡內存時引發。 |
名稱錯誤 | 當在局部或全局范圍內找不到變量時引發。 |
notimplemontederror | 由抽象方法引發。 |
操作系統錯誤 | 當系統操作導致系統相關錯誤時引發。 |
OverflowError | 當算術運算的結果太大而無法表示時引發。 |
報錯 | 當弱引用代理用于訪問垃圾回收引用時引發。 |
運行時錯誤 | 當錯誤不屬于任何其他類別時引發。 |
停止迭代 | 由 next()函數引發,表示迭代器不再返回任何項。 |
句法誤差 | 遇到語法錯誤時由解析器引發。 |
內建 Error | 當縮進不正確時引發。 |
TabError | 當縮進由不一致的制表符和空格組成時引發。 |
系統誤差 | 解釋器檢測到內部錯誤時引發。 |
系統退出 | 由 sys.exit()函數引發。 |
類型錯誤 | 當函數或操作應用于不正確類型的對象時引發。 |
unboundlocalherror | 當引用函數或方法中的局部變量,但沒有值綁定到該變量時引發。 |
UnicodeError 錯誤 | 發生與 Unicode 相關的編碼或解碼錯誤時引發。 |
unicodeencodererror | 編碼過程中出現與 Unicode 相關的錯誤時引發。 |
unicodedecodererror | 解碼過程中出現與 Unicode 相關的錯誤時引發。 |
unicode 翻譯錯誤 | 當轉換過程中出現與 Unicode 相關的錯誤時引發。 |
值錯誤 | 當函數獲得類型正確但值不正確的參數時引發。 |
零分割錯誤 | 當除法或模塊運算的第二個操作數為零時引發。 |
索引錯誤
試圖訪問無效索引處的項目時會拋出IndexError
。
Example: IndexError
>>> L1=[1,2,3]
>>> L1[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
L1[3]
IndexError: list index out of range
ModuleNotFoundError
找不到模塊時拋出ModuleNotFoundError
。
Example: ModuleNotFoundError
>>> import notamodule
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
import notamodule
ModuleNotFoundError: No module named 'notamodule'
鍵錯誤
找不到鑰匙時拋出KeyError
。
Example: KeyError
>>> D1={'1':"aa", '2':"bb", '3':"cc"}
>>> D1['4']
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
D1['4']
KeyError: '4'
導入錯誤
找不到指定函數時拋出ImportError
。
Example: ImportError
>>> from math import cube
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
from math import cube
ImportError: cannot import name 'cube'
停止迭代
當next()
函數超出迭代器項時,拋出StopIteration
。
Example: StopIteration
>>> it=iter([1,2,3])
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
next(it)
StopIteration
類型錯誤
當對不適當類型的對象應用操作或功能時,會拋出TypeError
。
Example: TypeError
>>> '2'+2
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
'2'+2
TypeError: must be str, not int
值錯誤
當函數的參數類型不合適時,會拋出ValueError
。
Example: ValueError
>>> int('xyz')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
名稱錯誤
找不到對象時拋出NameError
。
Example: NameError
>>> age
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
age
NameError: name 'age' is not defined
零分割錯誤
當除法中的第二個運算符為零時,拋出ZeroDivisionError
。
Example: ZeroDivisionError
>>> x=100/0
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
x=100/0
ZeroDivisionError: division by zero
鍵盤中斷
在程序執行過程中,當用戶點擊中斷鍵(通常是 Control-C)時,會拋出KeyboardInterrupt
。
Example: KeyboardInterrupt
>>> name=input('enter your name')
enter your name^c
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
name=input('enter your name')
KeyboardInterrupt
在下一章中學習如何用 Python 處理異常。***