日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python try/except/finally等

發(fā)布時(shí)間:2023/12/18 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python try/except/finally等 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python try/except/finally等

[代碼塊]
  • x?=?'abc'??
  • def?fetcher(obj,?index):??
  • ????return?obj[index]??
  • ??
  • fetcher(x,?4)??
  • 輸出:

  • ??File?"test.py",?line?6,?in?<module>??
  • ????fetcher(x,?4)??
  • ??File?"test.py",?line?4,?in?fetcher??
  • ????return?obj[index]??
  • IndexError:?string?index?out?of?range??

  • 第一:?try不僅捕獲異常,而且會(huì)恢復(fù)執(zhí)行

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????except:??
  • ????????print?"got?exception"??
  • ????print?"continuing"??
  • 輸出:

  • got?exception??
  • continuing??

  • 第二:無論try是否發(fā)生異常,finally總會(huì)執(zhí)行

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????finally:??
  • ????????print?'after?fecth'??
  • 輸出:

  • after?fecth??
  • Traceback?(most?recent?call?last):??
  • ??File?"test.py",?line?55,?in?<module>??
  • ????catcher()??
  • ??File?"test.py",?line?12,?in?catcher??
  • ????fetcher(x,?4)??
  • ??File?"test.py",?line?4,?in?fetcher??
  • ????return?obj[index]??
  • IndexError:?string?index?out?of?range??

  • 第三:try無異常,才會(huì)執(zhí)行else

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????except:??
  • ????????print?"got?exception"??
  • ????else:??
  • ????????print?"not?exception"??
  • 輸出:

  • got?exception??
  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?2)??
  • ????except:??
  • ????????print?"got?exception"??
  • ????else:??
  • ????????print?"not?exception"??
  • 輸出:

  • not?exception??
  • else作用:沒有else語句,當(dāng)執(zhí)行完try語句后,無法知道是沒有發(fā)生異常,還是發(fā)生了異常并被處理過了。通過else可以清楚的區(qū)分開。

    ?

    第四:利用raise傳遞異常

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????except:??
  • ????????print?"got?exception"??
  • ????????raise??
  • 輸出:

  • got?exception??
  • Traceback?(most?recent?call?last):??
  • ??File?"test.py",?line?37,?in?<module>??
  • ????catcher()??
  • ??File?"test.py",?line?22,?in?catcher??
  • ????fetcher(x,?4)??
  • ??File?"test.py",?line?4,?in?fetcher??
  • ????return?obj[index]??
  • IndexError:?string?index?out?of?range??
  • raise語句不包括異常名稱或額外資料時(shí),會(huì)重新引發(fā)當(dāng)前異常。如果希望捕獲處理一個(gè)異常,而又不希望

    異常在程序代碼中消失,可以通過raise重新引發(fā)該異常。

    ?

    第五:except(name1, name2)

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????except(TypeError,?IndexError):??
  • ????????print?"got?exception"??
  • ????else:??
  • ????????print?"not?exception"??
  • 捕獲列表列出的異常,進(jìn)行處理。若except后無任何參數(shù),則捕獲所有異常。

  • def?catcher():??
  • ????try:??
  • ????????fetcher(x,?4)??
  • ????except: ?
  • 轉(zhuǎn)載于:https://www.cnblogs.com/jp927/p/4535416.html

    總結(jié)

    以上是生活随笔為你收集整理的Python try/except/finally等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。