Python3學習筆記:異常代碼調試

什么是異常

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站建設、網(wǎng)站設計、寧河網(wǎng)絡推廣、小程序設計、寧河網(wǎng)絡營銷、寧河企業(yè)策劃、寧河品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供寧河建站搭建服務,24小時服務熱線:13518219792,官方網(wǎng)址:muchs.cn

程序運行過程中,產(chǎn)生的錯誤統(tǒng)稱為異常(bug)。這些異常有的可能是語法錯誤,如關鍵字輸入錯誤、調用錯誤等,這一類的異常都是顯式的,很好發(fā)現(xiàn);還有一種就是隱式的錯誤,只用在使用時才會被發(fā)現(xiàn),和使用者的操作有關。

下面介紹一下 Python 常見的異常提示:

異常類型.jpg

異常處理語句

try … except …
在使用時,將可能產(chǎn)生異常的代碼放在 try 語句中,把處理結果放在 except 語句中,這樣,當 try 后面的代碼發(fā)生錯誤時就會執(zhí)行 except 中的代碼。如果 try 后的代碼沒有異常,則不會執(zhí)行 except 后的代碼。

try:
do some things
except exceptionName:
do some things

exceptionName 用于指定可能出現(xiàn)的異常的名稱。如果不指定異常的名稱,則表示捕獲全部可能發(fā)生的異常。

示例代碼:

def division():
print("====== 開始分蘋果 ======")

apple = int(input("一共有幾個大蘋果:"))
child = int(input("一共有幾個小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個,剩余 {:d} 個。".format(apple, child, result, remain))
else:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個。".format(apple, child, result))

if name== "main":
try:
division()
except ZeroDivisionError:
print("蘋果不能被 0 個小朋友平分?。?!")

在捕獲異常的時候,可以同時捕獲多個異常,如:

try:
do some things
except(ValueError, ZeroDivisionError) as e:
do some things

try … except … else
該語句在 except 之后加了一個 else 語句,用于指定當 try 語句沒有發(fā)現(xiàn)異常時需要執(zhí)行的代碼,如果 try 語句中發(fā)現(xiàn)了異常,則不再執(zhí)行 else 之后的代碼。

try:
do some things
except exceptionName:
do some things
else:
do some things

示例代碼:

def division():
print("====== 開始分蘋果 ======")

apple = int(input("一共有幾個大蘋果:"))
child = int(input("一共有幾個小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個,剩余 {:d} 個。".format(apple, child, result, remain))
else:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個。".format(apple, child, result))

if name== "main":
try:
division()
except ZeroDivisionError:
print("蘋果不能被 0 個小朋友平分?。?!")
except ValueError as e:
print("輸入錯誤?。?!", e)
else:
print("蘋果分配成功。。。")

try … except … finally
無論 try 語句中是否發(fā)生異常,都會執(zhí)行 finally 之后的代碼。

try:
do some things
except exceptionName:
do some things
finally:
do some things

示例代碼:

def division():
print("====== 開始分蘋果 ======")

apple = int(input("一共有幾個大蘋果:"))
child = int(input("一共有幾個小朋友:"))

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個,剩余 {:d} 個。".format(apple, child, result, remain))
else:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個。".format(apple, child, result))

if name== "main":
try:
division()
except ZeroDivisionError:
print("蘋果不能被 0 個小朋友平分?。?!")
except ValueError as e:
print("輸入錯誤?。?!", e)
else:
print("蘋果分配成功。。。")
finally:
print("分配了一次蘋果。")

raise
如果某個函數(shù)可能會產(chǎn)生異常,但是不想在當前函數(shù)中處理該異常,則可以使用 raise 語句在函數(shù)中拋出異常,

raise [exceptionName[(reason)]]

其中,exceptionName[(reason)] 為可選參數(shù),用于指定拋出的異常名稱及異常信息的描述,如果省略則把異常原樣拋出。

示例代碼:

def division():
print("====== 開始分蘋果 ======")

apple = int(input("一共有幾個大蘋果:"))
child = int(input("一共有幾個小朋友:"))

if apple < child:
    raise ValueError("蘋果太少了不夠分!")

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個,剩余 {:d} 個。".format(apple, child, result, remain))
else:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個。".format(apple, child, result))

if name== "main":
try:
division()
except ZeroDivisionError:
print("蘋果不能被 0 個小朋友平分?。?!")
except ValueError as e:
print("輸入錯誤!??!", e)
else:
print("蘋果分配成功。。。")
finally:
print("分配了一次蘋果。")

程序測試

使用 IDE 調試

基本上所有的 IDE 都具有代碼調試功能,如 Python 自帶的 IDLE 和 PyCharm 等等。一般都是在出現(xiàn)異常的地方設置斷點,然后在此處查看數(shù)據(jù)的值是否正確。具體的內容我也在學習過程中,沒有可以調試的代碼,以后如果有資料了在進行補充。

使用 assert 語句調試

該語句一般用于對程序在某個時刻必須滿足的條件進行驗證,

1
assert expression [reason]

其中,expression 是一個條件表達式,如果為假則拋出 AssertError 異常,反之則什么都不做。reason 為可選參數(shù),用于描述前面的 expression 為了更好的知道哪里出現(xiàn)了錯誤。

示例代碼:

def division():
print("====== 開始分蘋果 ======")

apple = int(input("一共有幾個大蘋果:"))
child = int(input("一共有幾個小朋友:"))

assert apple > child, "蘋果不夠分。"

result = apple // child
remain = apple % child

if remain > 0:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個,剩余 {:d} 個。".format(apple, child, result, remain))
else:
    print("一共 {:d} 個大蘋果平均分給 {:d} 個小朋友,每人 {:d} 個。".format(apple, child, result))

if name== "main":
try:
division()
except ZeroDivisionError:
print("蘋果不能被 0 個小朋友平分?。?!")
except ValueError as e:
print("輸入錯誤?。?!", e)
else:
print("蘋果分配成功。。。")
finally:
print("分配了一次蘋果。")

分享文章:Python3學習筆記:異常代碼調試
新聞來源:http://muchs.cn/article18/jehggp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站收錄、品牌網(wǎng)站制作、網(wǎng)站排名、小程序開發(fā)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營