Python编程基础:第二十九节 异常Exception
第二十九節 異常Exception
- 前言
- 實踐
前言
我們在寫代碼時不可避免地會出錯,這時候編譯器便會拋出異常并中斷程序的執行。針對這種情況,我們可以采用異常處理的方式捕捉程序中的異常信息,并將異常信息打印出來供程序員分析,同時呢還要避免因為程序拋出異常而中斷其繼續向下執行。異常檢測代碼結構如下:
try:要檢測異常信息的代碼段 except 異常信息 as e:要打印的報錯信息供程序員分析 else:如果要檢測的代碼段沒有發生異常,那就執行這部分代碼 finally:無論程序是否有異常,這里的代碼均可以執行實踐
我們以分母為0為例學習異常處理的相關內容,眾所周知在除法中分母是不能為0的,若分母為0,程序便會拋出異常信息:
numerator = int(input("Enter a number to divide: ")) denominator = int(input("Enter a number to divide by: ")) result = numerator / denominator print(result) >>> Enter a number to divide: 5 >>> Enter a number to divide by: 0 >>> ZeroDivisionError: division by zero可見程序拋出了異常信息ZeroDivisionError并且中斷執行。此時我們便可以通過try-except結構來捕捉異常信息:
try:numerator = int(input("Enter a number to divide: "))denominator = int(input("Enter a number to divide by: "))result = numerator / denominator except ZeroDivisionError as e:print("You can't divide by zero! idiot!") >>> Enter a number to divide: 5 >>> Enter a number to divide by: 0 >>> You can't divide by zero! idiot!可見此時程序并沒有中斷,當其發現程序中有ZeroDivisionError時,便執行print("You can't divide by zero! idiot!")這行代碼用于提示程序員發生了除零錯誤。
那么如果我們不小心把除數寫成了字符串會發生什么呢?
可見代碼拋出了ValueError,提示程序員這里應該是int型數據而不是字符串,那么我們同樣可以使用異常處理方式捕捉這個異常信息:
try:numerator = int(input("Enter a number to divide: "))denominator = int(input("Enter a number to divide by: "))result = numerator / denominator except ZeroDivisionError as e:print("You can't divide by zero! idiot!") except ValueError as e:print("Enter only numbers plz") >>> Enter a number to divide: 5 >>> Enter a number to divide by: david >>> Enter only numbers plz不難發現程序捕捉到了這個異常信息,并打印出Enter only numbers plz,用于提醒用戶只能輸入數字,不能輸入字符串。其實Python中包含了許許多多的異常信息用于幫助程序員分析代碼,如果我們想讓代碼檢測所有的異常信息,那么就用到了下述代碼:
try:numerator = int(input("Enter a number to divide: "))denominator = int(input("Enter a number to divide by: "))result = numerator / denominator except ZeroDivisionError as e:# 用于打印程序自帶的異常信息print(e)print("You can't divide by zero! idiot!") except ValueError as e:print(e)print("Enter only numbers plz") except Exception as e:print(e)print("something went wrong :(")也就是不指定特定的異常信息,用Exception來代替,這樣做的好處就是程序即便發現了異常也不會中斷會繼續執行下去。這里需要注意的是,在判斷異常時,一定是先判斷是否是某一類異常,最后再判斷是否是一個異常信息。那么如果程序沒有異常時,我們希望它能執行的代碼放在else語句下邊:
try:numerator = int(input("Enter a number to divide: "))denominator = int(input("Enter a number to divide by: "))result = numerator / denominator except ZeroDivisionError as e:print(e)print("You can't divide by zero! idiot!") except ValueError as e:print(e)print("Enter only numbers plz") except Exception as e:print(e)print("something went wrong :(") else:print(result) >>> Enter a number to divide: 5 >>> Enter a number to divide by: 1 >>> 5.0可見,當我們的輸入都合法時,程序就會得到兩個數相除的計算結果并打印到控制臺。其實我們還可以書寫一些代碼,這些代碼無論程序是否發生異常,他們都會被執行,這些代碼需要放在finally的下面:
try:numerator = int(input("Enter a number to divide: "))denominator = int(input("Enter a number to divide by: "))result = numerator / denominator except ZeroDivisionError as e:print(e)print("You can't divide by zero! idiot!") except ValueError as e:print(e)print("Enter only numbers plz") except Exception as e:print(e)print("something went wrong :(") else:print(result) finally:print("This will always execute") >>> Enter a number to divide: 5 >>> Enter a number to divide by: 1 >>> 5.0 >>> This will always execute>>> Enter a number to divide: 5 >>> Enter a number to divide by: 0 >>> division by zero >>> You can't divide by zero! idiot! >>> This will always execute以上便是異常的全部內容,感謝大家的收藏、點贊、評論。我們下一節將介紹文件檢測(File Detection),敬請期待~
總結
以上是生活随笔為你收集整理的Python编程基础:第二十九节 异常Exception的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python编程基础:第二十八节 随机数
- 下一篇: Python编程基础:第三十节 文件检测