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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Python 異常處理 > 原文: [https://thepythonguru.com/python-exception-handling/](https://thepythonguru.com/python-exception-handling/) * * * 于 2020 年 1 月 7 日更新 * * * 異常處理使您能夠優雅地處理錯誤并對其進行有意義的處理。 如果未找到所需文件,則向用戶顯示一條消息。 Python 使用`try`和`except`塊處理異常。 **語法**: ```py try: ? ? # write some code ? ? # that might throw exception except <ExceptionType>: ? ? # Exception handler, alert the user ``` 如您在`try`塊中看到的那樣,您需要編寫可能引發異常的代碼。 當發生異常時,將跳過`try`塊中的代碼。 如果`except`子句中存在匹配的異常類型,則執行其處理器。 讓我們舉個例子: ```py try: ? ? f = open('somefile.txt', 'r') ? ? print(f.read()) ? ? f.close() except IOError: ? ? print('file not found') ``` 上面的代碼如下: 1. 執行`try`和`except`塊之間的第一條語句。 2. 如果沒有異常發生,則將跳過`except`子句下的代碼。 3. 如果文件不存在,則會引發異常,并且`try`塊中的其余代碼將被跳過 4. 發生異常時,如果異常類型與`except`關鍵字后的異常名稱匹配,則將執行該`except`子句中的代碼。 **注意**: 上面的代碼僅能處理`IOError`異常。 要處理其他類型的異常,您需要添加更多的`except`子句。 `try`語句可以具有多個`except`子句,也可以具有可選的`else`和/或`finally`語句。 ```py try: ? ? <body> except <ExceptionType1>: ? ? <handler1> except <ExceptionTypeN>: ? ? <handlerN> except: ? ? <handlerExcept> else: ? ? <process_else> finally: ? ? <process_finally> ``` `except`子句類似于`elif`。 發生異常時,將檢查該異常以匹配`except`子句中的異常類型。 如果找到匹配項,則執行匹配大小寫的處理器。 另請注意,在最后的`except`子句中,`ExceptionType`被省略。 如果異常不匹配最后一個`except`子句之前的任何異常類型,則執行最后一個`except`子句的處理器。 **注意**: `else`子句下的語句僅在沒有引發異常時運行。 **注意**: 無論是否發生異常,`finally`子句中的語句都將運行。 現在舉個例子。 ```py try: ? ? num1, num2 = eval(input("Enter two numbers, separated by a comma : ")) ? ? result = num1 / num2 ? ? print("Result is", result) except ZeroDivisionError: ? ? print("Division by zero is error !!") except SyntaxError: ? ? print("Comma is missing. Enter numbers separated by comma like this 1, 2") except: ? ? print("Wrong input") else: ? ? print("No exceptions") finally: ? ? print("This will execute no matter what") ``` **注意**: `eval()`函數允許 python 程序在其內部運行 python 代碼,`eval()`需要一個字符串參數。 要了解有關`eval()`的更多信息,請訪問 Python 中的[`eval()`](/python-builtin-functions/eval/)。 ## 引發異常 * * * 要從您自己的方法引發異常,您需要像這樣使用`raise`關鍵字 ```py raise ExceptionClass("Your argument") ``` 讓我們舉個例子 ```py def enterage(age): ? ? if age < 0: ? ? ? ? raise ValueError("Only positive integers are allowed") ? ? if age % 2 == 0: ? ? ? ? print("age is even") ? ? else: ? ? ? ? print("age is odd") try: ? ? num = int(input("Enter your age: ")) ? ? enterage(num) except ValueError: ? ? print("Only positive integers are allowed") except: ? ? print("something is wrong") ``` 運行程序并輸入正整數。 **預期輸出**: ```py Enter your age: 12 age is even ``` 再次運行該程序并輸入一個負數。 **預期輸出**: ```py Enter your age: -12 Only integers are allowed ``` ## 使用異常對象 * * * 現在您知道如何處理異常,在本節中,我們將學習如何在異常處理器代碼中訪問異常對象。 您可以使用以下代碼將異常對象分配給變量。 ```py try: ? ? # this code is expected to throw exception except ExceptionType as ex: ? ? # code to handle exception ``` 如您所見,您可以將異常對象存儲在變量`ex`中。 現在,您可以在異常處理器代碼中使用此對象。 ```py try: ? ? number = eval(input("Enter a number: ")) ? ? print("The number entered is", number) except NameError as ex: ? ? print("Exception:", ex) ``` 運行程序并輸入一個數字。 **預期輸出**: ```py Enter a number: 34 The number entered is 34 ``` 再次運行程序并輸入一個字符串。 **預期輸出**: ```py Enter a number: one Exception: name 'one' is not defined ``` ## 創建自定義異常類 * * * 您可以通過擴展`BaseException`類或`BaseException`的子類來創建自定義異常類。 ![python-exception-classes.jpg](https://img.kancloud.cn/8c/99/8c9926bd7447daf5337943440c5ab6b7_745x327.png) 如您所見,python 中的大多數異常類都是從`BaseException`類擴展而來的。 您可以從`BaseException`類或`BaseException`的子類(例如`RuntimeError`)派生自己的異常類。 創建一個名為`NegativeAgeException.py`的新文件,并編寫以下代碼。 ```py class NegativeAgeException(RuntimeError): ? ? def __init__(self, age): ? ? ? ? super().__init__() ? ? ? ? self.age = age ``` 上面的代碼創建了一個名為`NegativeAgeException`的新異常類,該異常類僅由使用`super().__init__()`調用父類構造器并設置`age`的構造器組成。 ## 使用自定義異常類 * * * ```py def enterage(age): ? ? if age < 0: ? ? ? ? raise NegativeAgeException("Only positive integers are allowed") ? ? if age % 2 == 0: ? ? ? ? print("age is even") ? ? else: ? ? ? ? print("age is odd") try: ? ? num = int(input("Enter your age: ")) ? ? enterage(num) except NegativeAgeException: ? ? print("Only positive integers are allowed") except: ? ? print("something is wrong") ``` 在下一篇文章中,我們將學習 [Python 模塊](/python-modules/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看