python異常舉例 Python異常處理

2021-10-16 15:18:26 字數 4324 閱讀 2563

1.1異常問題舉例

例一》 i = input('請輸入數字')

請輸入數字:0

>>> print(i)

>>> 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型別,所以丟擲異常

1.2異常類繼承層次

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.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

賦值和直接取值都不行,因為沒有這個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'

1.4對於捕獲異常的說明

我不會捕獲異常,不會使用try和except等等,看也看不懂啊……

python應用舉例 python基礎舉例應用

將下述兩個變數的值交換 s1 alex s2 sb s1,s2 s2,s1 print s1,s2 判斷下述結果 msg1 alex say my name is alex,my age is 73,my is female msg2 alex say my name is alex,my age ...

new delete以及異常處理的相關舉例

new 一般用法 new 型別 初值 比如 int i new int int j new int 100 int k new int 3 delete 一般用法 delete 指標變數 比如 delete i delete j delete k 簡單來說,即為乙個申請空間,乙個釋放空間,在不知陣列...

python 丟擲異常 python 異常

異常的概念 捕獲異常 異常的傳遞 丟擲異常 程式在執行時,如果 python 直譯器 遇到 到乙個錯誤,會停止程式的執行,並且提示一些錯誤資訊,這就是 異常 程式停止執行並且提示錯誤資訊 這個動作,我們通常稱之為 丟擲 raise 異常 程式開發時,很難將 所有的特殊情況 都處理的面面俱到,通過 異...