<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python 異常處理 python提供了兩個非常重要的功能來處理python程序在運行中出現的異常和錯誤。你可以使用該功能來調試python程序。 * 異常處理: 本站Python教程會具體介紹。 * 斷言(Assertions):本站Python教程會具體介紹。 ## python標準異常 | 異常名稱 | 描述 | | --- | --- | | BaseException | 所有異常的基類 | | SystemExit | 解釋器請求退出 | | KeyboardInterrupt | 用戶中斷執行(通常是輸入^C) | | Exception | 常規錯誤的基類 | | StopIteration | 迭代器沒有更多的值 | | GeneratorExit | 生成器(generator)發生異常來通知退出 | | SystemExit | Python 解釋器請求退出 | | StandardError | 所有的內建標準異常的基類 | | ArithmeticError | 所有數值計算錯誤的基類 | | FloatingPointError | 浮點計算錯誤 | | OverflowError | 數值運算超出最大限制 | | ZeroDivisionError | 除(或取模)零 (所有數據類型) | | AssertionError | 斷言語句失敗 | | AttributeError | 對象沒有這個屬性 | | EOFError | 沒有內建輸入,到達EOF 標記 | | EnvironmentError | 操作系統錯誤的基類 | | IOError | 輸入/輸出操作失敗 | | OSError | 操作系統錯誤 | | WindowsError | 系統調用失敗 | | ImportError | 導入模塊/對象失敗 | | KeyboardInterrupt | 用戶中斷執行(通常是輸入^C) | | LookupError | 無效數據查詢的基類 | | IndexError | 序列中沒有沒有此索引(index) | | KeyError | 映射中沒有這個鍵 | | MemoryError | 內存溢出錯誤(對于Python 解釋器不是致命的) | | NameError | 未聲明/初始化對象 (沒有屬性) | | UnboundLocalError | 訪問未初始化的本地變量 | | ReferenceError | 弱引用(Weak reference)試圖訪問已經垃圾回收了的對象 | | RuntimeError | 一般的運行時錯誤 | | NotImplementedError | 尚未實現的方法 | | SyntaxError | Python 語法錯誤 | | IndentationError | 縮進錯誤 | | TabError | Tab 和空格混用 | | SystemError | 一般的解釋器系統錯誤 | | TypeError | 對類型無效的操作 | | ValueError | 傳入無效的參數 | | UnicodeError | Unicode 相關的錯誤 | | UnicodeDecodeError | Unicode 解碼時的錯誤 | | UnicodeEncodeError | Unicode 編碼時錯誤 | | UnicodeTranslateError | Unicode 轉換時錯誤 | | Warning | 警告的基類 | | DeprecationWarning | 關于被棄用的特征的警告 | | FutureWarning | 關于構造將來語義會有改變的警告 | | OverflowWarning | 舊的關于自動提升為長整型(long)的警告 | | PendingDeprecationWarning | 關于特性將會被廢棄的警告 | | RuntimeWarning | 可疑的運行時行為(runtime behavior)的警告 | | SyntaxWarning | 可疑的語法的警告 | | UserWarning | 用戶代碼生成的警告 | ## 什么是異常? 異常即是一個事件,該事件會在程序執行過程中發生,影響了程序的正常執行。 一般情況下,在Python無法正常處理程序時就會發生一個異常。 異常是Python對象,表示一個錯誤。 當Python腳本發生異常時我們需要捕獲處理它,否則程序會終止執行。 ## 異常處理 捕捉異常可以使用try/except語句。 try/except語句用來檢測try語句塊中的錯誤,從而讓except語句捕獲異常信息并處理。 如果你不想在異常發生時結束你的程序,只需在try里捕獲它。 語法: 以下為簡單的_try....except...else_的語法: ``` try: <語句> #運行別的代碼 except <名字>: <語句> #如果在try部份引發了'name'異常 except <名字>,<數據>: <語句> #如果引發了'name'異常,獲得附加的數據 else: <語句> #如果沒有異常發生 ``` try的工作原理是,當開始一個try語句后,python就在當前程序的上下文中作標記,這樣當異常出現時就可以回到這里,try子句先執行,接下來會發生什么依賴于執行時是否出現異常。 * 如果當try后的語句執行時發生異常,python就跳回到try并執行第一個匹配該異常的except子句,異常處理完畢,控制流就通過整個try語句(除非在處理異常時又引發新的異常)。 * 如果在try后的語句里發生了異常,卻沒有匹配的except子句,異常將被遞交到上層的try,或者到程序的最上層(這樣將結束程序,并打印缺省的出錯信息)。 * 如果在try子句執行時沒有發生異常,python將執行else語句后的語句(如果有else的話),然后控制流通過整個try語句。 ### 實例 下面是簡單的例子,它打開一個文件,在該文件中的內容寫入內容,且并未發生異常: ``` #!/usr/bin/python try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find file or read data" else: print "Written content in the file successfully" fh.close() ``` 以上程序輸出結果: ``` Written content in the file successfully ``` ### 實例 下面是簡單的例子,它打開一個文件,在該文件中的內容寫入內容,但文件沒有寫入權限,發生了異常: ``` #!/usr/bin/python try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find file or read data" else: print "Written content in the file successfully" ``` 以上程序輸出結果: ``` Error: can't find file or read data ``` ## 使用except而不帶任何異常類型 你可以不帶任何異常類型使用except,如下實例: ``` try: You do your operations here; ...................... except: If there is any exception, then execute this block. ...................... else: If there is no exception then execute this block. ``` 以上方式try-except語句捕獲所有發生的異常。但這不是一個很好的方式,我們不能通過該程序識別出具體的異常信息。因為它捕獲所有的異常。 ## 使用except而帶多種異常類型 你也可以使用相同的except語句來處理多個異常信息,如下所示: ``` try: You do your operations here; ...................... except(Exception1[, Exception2[,...ExceptionN]]]): If there is any exception from the given exception list, then execute this block. ...................... else: If there is no exception then execute this block. ``` ## try-finally 語句 try-finally 語句無論是否發生異常都將執行最后的代碼。 ``` try: <語句> finally: <語句> #退出try時總會執行 raise ``` **注意:**你可以使用except語句或者finally語句,但是兩者不能同時使用。else語句也不能與finally語句同時使用 ### 實例 ``` #!/usr/bin/python try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") finally: print "Error: can\'t find file or read data" ``` 如果打開的文件沒有可寫權限,輸出如下所示: ``` Error: can't find file or read data ``` 同樣的例子也可以寫成如下方式: ``` #!/usr/bin/python try: fh = open("testfile", "w") try: fh.write("This is my test file for exception handling!!") finally: print "Going to close the file" fh.close() except IOError: print "Error: can\'t find file or read data" ``` 當在try塊中拋出一個異常,立即執行finally塊代碼。 finally塊中的所有語句執行后,異常被再次提出,并執行except塊代碼。 參數的內容不同于異常。 ## 異常的參數 一個異常可以帶上參數,可作為輸出的異常信息參數。 你可以通過except語句來捕獲異常的參數,如下所示: ``` try: You do your operations here; ...................... except ExceptionType, Argument: You can print value of Argument here... ``` 變量接收的異常值通常包含在異常的語句中。在元組的表單中變量可以接收一個或者多個值。 元組通常包含錯誤字符串,錯誤數字,錯誤位置。 ### 實例 以下為單個異常的實例: ``` #!/usr/bin/python # Define a function here. def temp_convert(var): try: return int(var) except ValueError, Argument: print "The argument does not contain numbers\n", Argument # Call above function here. temp_convert("xyz"); ``` 以上程序執行結果如下: ``` The argument does not contain numbers invalid literal for int() with base 10: 'xyz' ``` ### 觸發異常 我們可以使用raise語句自己觸發異常 raise語法格式如下: ``` raise [Exception [, args [, traceback]]] ``` 語句中Exception是異常的類型(例如,NameError)參數是一個異常參數值。該參數是可選的,如果不提供,異常的參數是"None"。 最后一個參數是可選的(在實踐中很少使用),如果存在,是跟蹤異常對象。 ### 實例 一個異常可以是一個字符串,類或對象。 Python的內核提供的異常,大多數都是實例化的類,這是一個類的實例的參數。 定義一個異常非常簡單,如下所示: ``` def functionName( level ): if level < 1: raise "Invalid level!", level # The code below to this would not be executed # if we raise the exception ``` **注意:**為了能夠捕獲異常,"except"語句必須有用相同的異常來拋出類對象或者字符串。 例如我們捕獲以上異常,"except"語句如下所示: ``` try: Business Logic here... except "Invalid level!": Exception handling here... else: Rest of the code here... ``` ## 用戶自定義異常 通過創建一個新的異常類,程序可以命名它們自己的異常。異常應該是典型的繼承自Exception類,通過直接或間接的方式。 以下為與RuntimeError相關的實例,實例中創建了一個類,基類為RuntimeError,用于在異常觸發時輸出更多的信息。 在try語句塊中,用戶自定義的異常后執行except塊語句,變量 e 是用于創建Networkerror類的實例。 ``` class Networkerror(RuntimeError): def __init__(self, arg): self.args = arg ``` 在你定義以上類后,你可以觸發該異常,如下所示: ``` try: raise Networkerror("Bad hostname") except Networkerror,e: print e.args ```
                  <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>

                              哎呀哎呀视频在线观看