<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 異常和錯誤 --- [TOC] ## 錯誤類型 |code|錯誤類型| |---|---| |a NameError |未定義變量| |if True SyntaxError | 語法錯誤| |f =open('1.txt') | IOError -IO錯誤| |10/0 |ZeroDivisionError | |a = int('ab') | ValueError| ## 捕獲一種錯誤并打印 try: a except NameError as e: print("catch Error",e) ## 捕獲所有錯誤 try: 10/0 except NameError as e: print("catch Error",e) except Exception as e: print('all error') ## else try: 10/1 except ValueError as e: print("catch Error",e) else: print("success") >若沒有錯誤則打印 success ## try-finally try: f=open("1.txt") print(int(f.read())) finally: print("file close") f.close() >1. 規則: try-finally 無論是否檢測到異常,都會執行finally代碼 >2. 作用: 為異常處理時間提供清理機制,用來關閉文件或者釋放系統資源:如果存在異常,先處理異常,在處理finally ## try-except-else-finally try: f=open("./template/hello1.html") except NameError as e: print("NameError:",e) else: print("no Error") finally: print("file close") f.close() #輸出 no Error file close ## with ### with context [as var] >1. with 語句用來代替try-except_final 語句 ,使代碼更加簡潔 >文件打開推薦使用with ,他可以自動關閉文件 ### with 語句實質是上下文管理 >1. 上下文管理協議:包含__enter__()和__exit__(),支持該協議必須該對象支持此方法 >2. 上下文管理器:定義執行with語句時要簡歷的運行時上下文,負責執行with語句塊上下文中的進入與推出操作 >3. 進入上下文管理器:調用管理器__enter__方法,日過設置 as var 語句 var變量接受__enter__()方法返回值 ## raise 主動拋出異常 >語法格式 raise [exception[,args]] >exception : 異常類 ,如 ValueError,TypeError python 2.x 寫法 raise TypeError,"error" python 3.x寫法 raise TypeError("errormessage") --- try: raise TypeError("error message") raise Exception("普通錯誤類型") except TypeError as e: print("TypeError:",e) except Exception as e: print('Exception :'+str(e)) #輸出:TypeError : error message ### 重新拋出異常 def test_exception(): try: print(noname) except Exception as e: raise e #重新拋出異常,錯誤類型不變 try: test_exception() except NameError as e: print(e) ## assert語句 判斷是否為真 >斷言語句 : 如果為假,引發AssertionError錯誤 >語法格式: assert expression[,args] > args : 判斷條件的描述信息 python3 實例 ```python try: a='98' assert (isinstance(a,int)), ' is\'not int!' except AssertionError as e: print("AssertionError:",e) #輸出 AssertionError:is'not int ``` ## 自定義異常類 ```python class CustomError (Exception): def __init__(self,info): Exception.__init__(self) self.errorinfo=info def __str__(self): return "CustomError:%s" % self.errorinfo try: raise CustomError("test custom Error") except CustomError as e: print("Error:",e) #輸出 CustomError:test custom Error ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看