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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

多个错误多个tryexcept的处理异常处理

發布時間:2023/11/28 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多个错误多个tryexcept的处理异常处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
		</table></div><!-- /download --><div class="markdown-body"><!-- 歡迎成為極客學院WIKI作者 --><!-- wiki原創作者 --><p class="author"><a href="https://github.com/eastlakeside/" class="author-name right-top-img" data-name="訪問原作者" target="_blank">東濱社</a> · 更新于 2018-11-28 11:00:43</p><!-- 內容 --><h1>處理多個異常</h1>
離線下載
獲取電子書上線通知

我們可以使用三種方法來處理多個異常。

第一種方法需要把所有可能發生的異常放到一個元組里。像這樣:

try:file = open('test.txt', 'rb')
except (IOError, EOFError) as e:print("An error occurred. {}".format(e.args[-1]))

另外一種方式是對每個單獨的異常在單獨的except語句塊中處理。我們想要多少個except語句塊都可以。這里是個例子:

try:file = open('test.txt', 'rb')
except EOFError as e:print("An EOF error occurred.")raise e
except IOError as e:print("An error occurred.")raise e

上面這個方式中,如果異常沒有被第一個except語句塊處理,那么它也許被下一個語句塊處理,或者根本不會被處理。

現在,最后一種方式會捕獲所有異常:

try:file = open('test.txt', 'rb')
except Exception:# 打印一些異常日志,如果你想要的話raise

當你不知道你的程序會拋出什么樣的異常時,上面的方式可能非常有幫助。

	<div class="previous-and-next"><span class="pull-left">上一篇: <a id="previous_link" href="/project/interpy-zh/exception/README.html" data-toggle="tooltip" data-placement="top" title="" data-original-title="上翻頁快捷鍵:左方向鍵">異常</a></span><span class="pull-right">下一篇: <a id="next_link" href="/project/interpy-zh/exception/finally_clause.html" data-toggle="tooltip" data-placement="top" title="" data-original-title="下翻頁快捷鍵:右方向鍵">finally從句</a></span></div><div class="detail-share clearfix"><div class="bdsharebuttonbox share-item bdshare-button-style0-16" data-bd-bind="1568947926204"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空間"></a><a href="#" class="bds_youdao" data-cmd="youdao" title="分享到有道云筆記"></a></div><script>window._bd_share_config = {"common": {"bdSnsKey": {},"bdText": "","bdMini": "2","bdMiniList": false,"bdPic": "","bdStyle": "0","bdSize": "16"},"share": {},"image": {"viewList": ["weixin", "tsina", "qzone", "youdao"],"viewText": "分享到:","viewSize": "16"}};with(document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = '/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script></div><!--廣告--><!--/廣告--><!-- 評論 --><div id="jkcomments"><ul></ul><div id="pager"></div><div class="jkxy-subask-nologin">只有登錄了才能參與評論,快<a href="http://passport.jikexueyuan.com/sso/login">登錄</a>!如果你還沒有賬號你可以<a href="http://passport.jikexueyuan.com/sso/reg_phone">注冊</a>一個賬號。</div></div><!-- 新 footer --><div class="wiki-footer"><ul><li><a href="//help.jikexueyuan.com/" target="_blank">關于我們</a></li><li><a href="//help.jikexueyuan.com/join.html" target="_blank">加入我們</a></li><li><a href="//help.jikexueyuan.com/contact.html" target="_blank">聯系我們</a></li><li><a href="//j.jikexueyuan.com/evangelist/apply" target="_blank">講師合作</a></li><li><a href="//help.jikexueyuan.com/" target="_blank">幫助中心</a></li><li><a href="//blog.jikexueyuan.com/" target="_blank">黑板報</a></li><li><a href="//www.jikexueyuan.com/friendlink.html" target="_blank">友情鏈接</a></li><li><a href="javascript:void(0)" onclick="meiqia()" target="_blank">意見反饋</a></li></ul></div>
</div>

總結

以上是生活随笔為你收集整理的多个错误多个tryexcept的处理异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。