<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 引發異常 你可以使用`raise`語句 引發 異常。你還得指明錯誤/異常的名稱和伴隨異常 觸發的 異常對象。你可以引發的錯誤或異常應該分別是一個`Error`或`Exception`類的直接或間接導出類。 ``` #!/usr/bin/python # Filename: raising.py class ShortInputException(Exception): ????'''A user-defined exception class.''' ????def __init__(self, length, atleast): ????????Exception.__init__(self) ????????self.length = length ????????self.atleast = atleast try: ????s = raw_input('Enter something --&gt; ') ????if len(s) &lt; 3: ????????raise ShortInputException(len(s), 3) ????# Other work can continue as usual here except EOFError: ????print '\nWhy did you do an EOF on me?' except ShortInputException, x: ????print 'ShortInputException: The input was of length %d, \ ??????????was expecting at least %d' % (x.length, x.atleast) else: ????print 'No exception was raised.' ``` 源文件([code/raising.py](code/raising.py)) ## 輸出 ``` $ python raising.py Enter something --&gt; Why did you do an EOF on me? $ python raising.py Enter something --&gt; ab ShortInputException: The input was of length 2, was expecting at least 3 $ python raising.py Enter something --&gt; abc No exception was raised. ``` ## 它如何工作 這里,我們創建了我們自己的異常類型,其實我們可以使用任何預定義的異常/錯誤。這個新的異常類型是`ShortInputException`類。它有兩個域——`length`是給定輸入的長度,`atleast`則是程序期望的最小長度。 在`except`從句中,我們提供了錯誤類和用來表示錯誤/異常對象的變量。這與函數調用中的形參和實參概念類似。在這個特別的`except`從句中,我們使用異常對象的`length`和`atleast`域來為用戶打印一個恰當的消息。
                  <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>

                              哎呀哎呀视频在线观看