python異常型別列表

2021-08-28 09:41:27 字數 2332 閱讀 8885

1. nameerror:嘗試訪問乙個未申明的變數

>>> v

nameerror: name 'v' is not defined

2. zerodivisionerror:除數為0

>>> v = 1/0

zerodivisionerror: int division or modulo by zero

3. syntaxerror:語法錯誤

int int

syntaxerror: invalid syntax (, line 1)

4. indexerror:索引超出範圍

list = [2]

>>> list[3]

traceback (most recent call last):

file "", line 1, in list[3]

indexerror: list index out of range

5. keyerror:字典關鍵字不存在

dic = 

>>> dic['3']

traceback (most recent call last):

file "", line 1, in dic['3']

keyerror: '3'

6. ioerror:輸入輸出錯誤

>>> f = open('abc')

ioerror: [errno 2] no such file or directory: 'abc'

7. attributeerror:訪問未知物件屬性

>>> class worker:

def work():

print("i am working")

>>> w = worker()

>>> w.a

traceback (most recent call last):

file "", line 1, in w.a

attributeerror: 'worker' object has no attribute 'a'

8.valueerror:數值錯誤

>>> int('d')

traceback (most recent call last):

file "", line 1, in int('d')

valueerror: invalid literal for int() with base 10: 'd'

9. typeerror:型別錯誤

>>> istr = '22'

>>> ival = 22

>>> obj = istr + ival;

traceback (most recent call last):

file "", line 1, in obj = istr + ival;

typeerror: can't convert 'int' object to str implicitly

10. assertionerror:斷言錯誤

>>> assert 1 != 1

traceback (most recent call last):

file "", line 1, in assert 1 != 1

assertionerror

11.memoryerror:記憶體耗盡異常

12. notimplementederror:方法沒實現引起的異常

class base(object):

def __init__(self):

pass

def action(self):

#丟擲異常,說明該介面方法未實現

raise notimplementederror

13. lookuperror:鍵、值不存在引發的異常

lookuperror異常是indexerror、keyerror的基類, 如果你不確定資料型別是字典還是列表時,可以用lookuperror捕獲此異常

14. standarderror 標準異常

除stopiteration, generatorexit, keyboardinterrupt 和systemexit外,其他異常都是standarerror的子類。

錯誤檢測與異常處理區別在於:錯誤檢測是在正常的程式流中,處理不可預見問題的**,例如乙個呼叫操作未能成功結束

python異常類 python 異常型別

1 nameerror 嘗試訪問乙個未申明的變數 v nameerror name v is not defined 2 zerodivisionerror 除數為0 v 1 0 zerodivisionerror int division or modulo by zero 3 syntaxerr...

python列表型別如何 python列表型別

列表型別 一 列表 list 如果現在有乙個需求,我們需要儲存乙個人的愛好,用前面說的資料型別儲存,毫無疑問只能用字串儲存,並且乙個人的愛好可能是很多個,那麼我們可以用空格將讓他們分開。hobbies read run girl print hobbies 輸出 read run girl 我們一直...

Python異常處理和異常型別

try some functions.except exception,e print e try some functions.except exception as e print e 注意這裡exception,e變成了exception as e 1.nameerror 嘗試訪問乙個未申明的...