Python中的異常型別

2021-04-28 07:50:28 字數 1661 閱讀 7262

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.aattributeerror: '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

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 異常型別

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異常處理和異常型別

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