新聞中心
Python 程序中最常見的錯誤原因是某個語句不符合規(guī)定的用法。這種錯誤稱為語法錯誤。Python 解釋器會立即報告它,通常會附上原因。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供扶余網(wǎng)站建設(shè)、扶余做網(wǎng)站、扶余網(wǎng)站設(shè)計、扶余網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、扶余企業(yè)網(wǎng)站模板建站服務(wù),十年扶余做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
Example: Error
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
在 Python 3.x 中,print 是一個內(nèi)置函數(shù),需要括號。上面的語句違反了這種用法,因此會顯示語法錯誤。
但是很多時候,程序在運(yùn)行后會導(dǎo)致錯誤,即使它沒有任何語法錯誤。這種錯誤是運(yùn)行時錯誤,稱為異常。Python 庫中定義了許多內(nèi)置的異常。讓我們看看一些常見的錯誤類型。
下表列出了 Python 中重要的內(nèi)置異常。
| 例外 | 描述 |
|---|---|
| 斷言錯誤 | assert 語句失敗時引發(fā)。 |
| 屬性錯誤 | 對屬性賦值或引用引發(fā)的。 |
| 歐費(fèi)羅 | 當(dāng) input()函數(shù)達(dá)到文件結(jié)束條件時引發(fā)。 |
| 浮動指針錯誤 | 浮點(diǎn)運(yùn)算失敗時引發(fā)。 |
| GeneratorExit | 調(diào)用生成器的 close()方法時引發(fā)。 |
| 導(dǎo)入錯誤 | 找不到導(dǎo)入的模塊時引發(fā)。 |
| 索引錯誤 | 當(dāng)序列的索引超出范圍時引發(fā)。 |
| 鍵錯誤 | 在字典中找不到鍵時引發(fā)。 |
| 鍵盤中斷 | 當(dāng)用戶點(diǎn)擊中斷鍵(Ctrl+c 或 delete)時引發(fā)。 |
| 存儲器錯誤 | 當(dāng)操作耗盡內(nèi)存時引發(fā)。 |
| 名稱錯誤 | 當(dāng)在局部或全局范圍內(nèi)找不到變量時引發(fā)。 |
| notimplemontederror | 由抽象方法引發(fā)。 |
| 操作系統(tǒng)錯誤 | 當(dāng)系統(tǒng)操作導(dǎo)致系統(tǒng)相關(guān)錯誤時引發(fā)。 |
| OverflowError | 當(dāng)算術(shù)運(yùn)算的結(jié)果太大而無法表示時引發(fā)。 |
| 報錯 | 當(dāng)弱引用代理用于訪問垃圾回收引用時引發(fā)。 |
| 運(yùn)行時錯誤 | 當(dāng)錯誤不屬于任何其他類別時引發(fā)。 |
| 停止迭代 | 由 next()函數(shù)引發(fā),表示迭代器不再返回任何項(xiàng)。 |
| 句法誤差 | 遇到語法錯誤時由解析器引發(fā)。 |
| 內(nèi)建 Error | 當(dāng)縮進(jìn)不正確時引發(fā)。 |
| TabError | 當(dāng)縮進(jìn)由不一致的制表符和空格組成時引發(fā)。 |
| 系統(tǒng)誤差 | 解釋器檢測到內(nèi)部錯誤時引發(fā)。 |
| 系統(tǒng)退出 | 由 sys.exit()函數(shù)引發(fā)。 |
| 類型錯誤 | 當(dāng)函數(shù)或操作應(yīng)用于不正確類型的對象時引發(fā)。 |
| unboundlocalherror | 當(dāng)引用函數(shù)或方法中的局部變量,但沒有值綁定到該變量時引發(fā)。 |
| UnicodeError 錯誤 | 發(fā)生與 Unicode 相關(guān)的編碼或解碼錯誤時引發(fā)。 |
| unicodeencodererror | 編碼過程中出現(xiàn)與 Unicode 相關(guān)的錯誤時引發(fā)。 |
| unicodedecodererror | 解碼過程中出現(xiàn)與 Unicode 相關(guān)的錯誤時引發(fā)。 |
| unicode 翻譯錯誤 | 當(dāng)轉(zhuǎn)換過程中出現(xiàn)與 Unicode 相關(guān)的錯誤時引發(fā)。 |
| 值錯誤 | 當(dāng)函數(shù)獲得類型正確但值不正確的參數(shù)時引發(fā)。 |
| 零分割錯誤 | 當(dāng)除法或模塊運(yùn)算的第二個操作數(shù)為零時引發(fā)。 |
索引錯誤
試圖訪問無效索引處的項(xiàng)目時會拋出IndexError。
Example: IndexError
>>> L1=[1,2,3]
>>> L1[3]
Traceback (most recent call last):
File "", line 1, in
L1[3]
IndexError: list index out of range
ModuleNotFoundError
找不到模塊時拋出ModuleNotFoundError。
Example: ModuleNotFoundError
>>> import notamodule
Traceback (most recent call last):
File "", line 1, in
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 "", line 1, in
D1['4']
KeyError: '4'
導(dǎo)入錯誤
找不到指定函數(shù)時拋出ImportError。
Example: ImportError
>>> from math import cube
Traceback (most recent call last):
File "", line 1, in
from math import cube
ImportError: cannot import name 'cube'
停止迭代
當(dāng)next()函數(shù)超出迭代器項(xiàng)時,拋出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 "", line 1, in
next(it)
StopIteration
類型錯誤
當(dāng)對不適當(dāng)類型的對象應(yīng)用操作或功能時,會拋出TypeError。
Example: TypeError
>>> '2'+2
Traceback (most recent call last):
File "", line 1, in
'2'+2
TypeError: must be str, not int
值錯誤
當(dāng)函數(shù)的參數(shù)類型不合適時,會拋出ValueError。
Example: ValueError
>>> int('xyz')
Traceback (most recent call last):
File "", line 1, in
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
名稱錯誤
找不到對象時拋出NameError。
Example: NameError
>>> age
Traceback (most recent call last):
File "", line 1, in
age
NameError: name 'age' is not defined
零分割錯誤
當(dāng)除法中的第二個運(yùn)算符為零時,拋出ZeroDivisionError。
Example: ZeroDivisionError
>>> x=100/0
Traceback (most recent call last):
File "", line 1, in
x=100/0
ZeroDivisionError: division by zero
鍵盤中斷
在程序執(zhí)行過程中,當(dāng)用戶點(diǎn)擊中斷鍵(通常是 Control-C)時,會拋出KeyboardInterrupt。
Example: KeyboardInterrupt
>>> name=input('enter your name')
enter your name^c
Traceback (most recent call last):
File "", line 1, in
name=input('enter your name')
KeyboardInterrupt
在下一章中學(xué)習(xí)如何用 Python 處理異常。***
分享標(biāo)題:Python錯誤類型
文章轉(zhuǎn)載:http://m.5511xx.com/article/dpsposp.html


咨詢
建站咨詢
