Python的異常處理

2022-04-07 18:02:18 字數 4693 閱讀 3870

異常處理

目錄1.4對於捕獲異常的說明

例一

>>> i = input('請輸入數字')

請輸入數字:0

>>> print(i)

0>>> print(5 / int(i))

traceback (most recent call last):

file "", line 1, in zerodivisionerror: division by zero

上述**的報錯是除零的錯誤,數學上不允許除零

例二

>>> i="qwe"

>>> print(5 / int(i))

traceback (most recent call last):

file "", line 1, in valueerror: invalid literal for int() with base 10: 'qwe'

i是字串型別,而且是qwe,不能轉換成int型別,所以丟擲異常

python中的異常根類是baseexception,結構圖如下

baseexception

+-- systemexit

+-- keyboardinterrupt

+-- generatorexit

+-- exception

+-- stopiteration

+-- standarderror

| +-- buffererror

| +-- arithmeticerror

| | +-- floatingpointerror

| | +-- overflowerror

| | +-- zerodivisionerror

| +-- assertionerror

| +-- attributeerror

| +-- environmenterror

| | +-- ioerror

| | +-- oserror

| | +-- windowserror (windows)

| | +-- vmserror (vms)

| +-- eoferror

| +-- importerror

| +-- lookuperror

| | +-- indexerror

| | +-- keyerror

| +-- memoryerror

| +-- nameerror

| | +-- unboundlocalerror

| +-- referenceerror

| +-- runtimeerror

| | +-- notimplementederror

| +-- syntaxerror

| | +-- indentationerror

| | +-- taberror

| +-- systemerror

| +-- typeerror

| +-- valueerror

| +-- unicodeerror

| +-- unicodedecodeerror

| +-- unicodeencodeerror

| +-- unicodetranslateerror

+-- warning

+-- deprecationwarning

+-- pendingdeprecationwarning

+-- runtimewarning

+-- syntaxwarning

+-- userwarning

+-- futurewarning

+-- importwarning

+-- unicodewarning

+-- byteswarning

1.3.1 attributeerror 異常

訪問不存在的成員

例如

>>> class animal(object):

pass

>>> a1 = animal()

>>> a1.run()

traceback (most recent call last):

file "", line 1, in a1.run()

attributeerror: 'animal' object has no attribute 'run'

>>>

>>> print(a1.age)

traceback (most recent call last):

file "", line 1, in print(a1.age)

attributeerror: 'animal' object has no attribute 'age'

程式中建立了乙個類叫animal,但是並沒有run函式和age變數,所以丟擲異常

1.3.2 oserror 異常

oserror是作業系統相關異常,例如試圖開啟乙個不存在的檔案

>>> f=open("abc.txt")

traceback (most recent call last):

file "", line 1, in f=open("abc.txt")

filenotfounderror: [errno 2] no such file or directory: 'abc.txt'

1.3.3 indexerror 異常

是訪問元素時,下標超出索引最大值或最小值引發異常例如:

>>> code_list = [125, 56, 89, 36]

>>> code_list[4]

traceback (most recent call last):

file "", line 1, in code_list[4]

indexerror: list index out of range

1.3.4 keyerror 異常

是試圖訪問字典裡不存在的鍵引發,例如:

>>> dict1[104]

traceback (most recent call last):

file "", line 1, in dict1[104]

nameerror: name 'dict1' is not defined

104在字典中不存在,o(∩_∩)o哈哈~那就報錯吧

1.3.5 nameerror 異常

nameerror是指試圖使用乙個不存在的變數引發的異常,pythonshell中執行例項

>>> value1

traceback (most recent call last):

file "", line 1, in nameerror: name 'value1' is not defined

>>> a = value1

traceback (most recent call last):

file "", line 1, in nameerror: name 'value1' is not defined

>>> value1 = 10

>>> value1

10

賦值和直接取值都不行,因為沒有這個value1變數

只有最後的value1 = 10,才讓value1有了值

1.3.6 typeerror 異常

typeerror是試圖傳入變數型別與要求的不符合時而引發的異常。pythonshell執行例項、

>>> i = '2'

>>> print(5 / i)

traceback (most recent call last):

file "", line 1, in print(5 / i)

typeerror: unsupported operand type(s) for /: 'int' and 'str'

i是乙個字元型別,而5是整形,不能進行除法運算,因為型別不統一

1.3.7 valueerror 異常

valueerror是由於傳入乙個無效的引數值而引發的異常,這個異常在前面已經遇到了

>>> del i

>>> i = 'qwe'

>>> print( 5 / int(i))

traceback (most recent call last):

file "", line 1, in print( 5 / int(i))

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

我不會捕獲異常,不會使用tryexcept等等,看也看不懂啊……
python異常處理 Python 異常處理

使用者輸入不完整 比如輸入為空 或者輸入非法 輸入不是數字 異常就是程式執行時發生錯誤的訊號,在python中,錯誤觸發的異常如下 在python中不同的異常可以用不同的型別 python中統一了類與型別,型別即類 去標識,不同的類物件標識不同的異常,乙個異常標識一種錯 觸發indexerror 觸...

Python的異常處理

python中的異常型別分如下幾種 1 nameerror 嘗試訪問乙個未申明的變數 v nameerror name v is not defined 2 zerodivisionerror 除數為0 v 1 0 zerodivisionerror int division or modulo b...

Python的異常處理

1.raise語句 為了引發異常,可以使用乙個類或者例項呼叫raise語句。raise exceptiontraceback most recent call last file line 1,in exception raise exception hyperdive overload trace...