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

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


咨詢
建站咨詢
