Python學習筆記之異常

2022-07-21 02:15:09 字數 3697 閱讀 2957

python用異常物件來表示異常情況,如果異常物件未被處理或捕捉,程式就會回溯(traceback)中止執行。

異常可以在出錯時自動引發,也可以主動引發。

異常被引發後如果不被處理就會傳播至程式呼叫的地方,直到主程式(全域性作用域),如果主程式仍然沒有異常處理,程式會帶著棧跟蹤終止。

raise:引發異常

>>> raise

exception

traceback (most recent call last):

file

"", line 1, in

raise

exception

exception

>>> raise exception("error!!!")

traceback (most recent call last):

file "", line 1, in

raise exception("error!!!")

exception: error!!!

常見內建異常類:

類名描述

exception

所有異常的基類

attributeerror

特性引用或賦值失敗時引發

ioerror

試圖開啟不存在檔案(包括其他情況)時引發

indexerror

在使用序列中不存在的索引時引發

keyerror

使用對映中不存在的鍵引發

nameerror

找不到名字(變數)時引發

syntaxerror

在**為錯誤形式時引發

typeerror

在內建操作或者函式應用於錯誤型別的物件引發 

valueerror

在內建操作或者函式應用於正確的物件,但是該物件使用不合適的值引發

zerodivision

在除法或者模除操作的第二個引數為0時引發

自定義異常類:繼承自exception

class defexception(exception):pass

捕捉異常:使用try/except語句實現》 try

:

x =  int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

zerodivisionerror:

print("

error")

the first num:5the second num:0

error

>>> try

: x = int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

zerodivisionerror:

print("

error")

except

valueerror:

print("

typeerror")

the first num:5the second num:o

typeerror

用乙個塊捕捉多個異常:

>>> try

: x = int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

(zerodivisionerror,valueerror):

print("

error")

the first num:5the second num:0

error

捕捉物件:

>>> try

: x = int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

(zerodivisionerror,valueerror) as e:

print

(e)

the first num:5the second num:0

division by zero

捕捉所有異常:

try

: x = int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

:

print("

some errors")

the first num:5the second num:

some errors

這種方式會捕捉使用者中止執行的企圖,會隱藏所有程式設計師未想到並且未做好準備的錯誤。

對於異常情況進行處理:

#

在輸入不合法時迴圈,直到合法值出現退出迴圈

while

true:

try:

x = int(input("

the first num:"))

y = int(input("

the second num:"))

print(x/y)

except

:

print("

error")

else

:

break

#執行結果

the first num:5the second num:0

error

the first num:6the second num:3

2.0

finally子句:用在可能的異常後進行清理,不管是否有異常都要執行。在同乙個try語句中,不可以和except使用。

x =none

try:

x = 1/0

finally

:

print("

cleaning")

delx#結果

cleaning

traceback (most recent call last):

file

"input.py

", line 4, in

x = 1/0

zerodivisionerror: division by zero

***repl closed***

可以在一條語句中組合使用try,except,else,finally

try

: x = 1/0

else

:

print("

done")

finally

:

print("

cleaning")

#執行結果

cleaning

***repl closed***

python學習筆記之異常處理

目錄x 10 try y int input 數字 10 y 10 y y 10 except exception as e print f error 033 1 35m 033 0m finally 無論包不報錯,都會執行finally下面的 print 1 作業 使用while迴圈輸出1 2 ...

Python 學習筆記之檔案和異常

python中對檔案 資料夾 檔案操作函式 的操作需要涉及到os模組和shutil模組。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedi...

Python 異常 學習筆記

python 標準異常總結 try 和 try finally 語句 try 下面是檢測範圍,如發生異常停止在第乙個異常的位置 fh open testfile w fh.write 這是乙個測試檔案,用於測試異常 except oserror as reason 下面是出現異常後輸出的 print...