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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## Exceptions 你的Dart代碼可以拋出和捕獲異常。異常是指程序執行中發生的意料之外的錯誤。如果沒有捕獲異常,引發異常的隔離程序將被掛起,通常隔離程序及其程序將被終止。 與Java相反,Dart的所有異常都是未檢查的異常。方法不聲明它們可能拋出哪些異常,也不要求您捕獲任何異常。 Dart提供Exception和Error類型,以及許多預定義的子類型。當然你可以自己定義異常。但是,Dart程序可以拋出任何非空對象不僅僅是異常和錯誤對象。 ## Throw 這里有一個拋出或掛起異常的例子: ~~~ throw FormatException('Expected at least 1 section'); ~~~ 你也可以拋出任意對象: ~~~ throw 'Out of llamas!'; ~~~ >注意:在正式使用中通常拋出的是實現了Error或Exception類型的對象; > 因為拋出異常是一個 ***表達式*** ,您可以在=>語句中拋出異常,也可以在任何允許表達式的地方拋出異常: ~~~ void distanceTo(Point other) => throw UnimplementedError(); ~~~ ## Catch 捕獲異常將阻止異常傳播(除非重新拋出異常)。捕獲異常后我們可以去判斷并處理相應的異常。 ~~~ try { breedMoreLlamas(); } on OutOfLlamasException { buyMoreLlamas(); } ~~~ 要處理可以拋出多種異常類型的代碼,可以指定多個catch子句。與拋出對象的類型匹配的第一個catch子句處理異常。如果catch子句沒有指定類型,則該子句可以處理任何類型的拋出對象: ~~~ try { breedMoreLlamas(); } on OutOfLlamasException { // A specific exception buyMoreLlamas(); } on Exception catch (e) { // Anything else that is an exception print('Unknown exception: $e'); } catch (e) { // No specified type, handles all print('Something really unknown: $e'); } ~~~ 正如前面的代碼所示,您可以使用on或catch或both。在需要指定異常類型時使用。當您的異常處理程序需要異常對象時,請使用catch。 可以指定catch()的一個或兩個參數。第一個是拋出的異常,第二個是堆棧跟蹤(StackTrace對象)。 ~~~ try { // ··· } on Exception catch (e) { print('Exception details:\n $e'); } catch (e, s) { print('Exception details:\n $e'); print('Stack trace:\n $s'); } ~~~ 要在捕獲中處理異常,同時允許其繼續傳播,請使用rethrow關鍵字。 ~~~ void misbehave() { try { dynamic foo = true; print(foo++); // Runtime error } catch (e) { print('misbehave() partially handled ${e.runtimeType}.'); rethrow; // Allow callers to see the exception. } } void main() { try { misbehave(); } catch (e) { print('main() finished handling ${e.runtimeType}.'); } } ~~~ ## Finally 要確保在拋出異常時運行某些業務代碼,請使用finally子句。如果沒有catch子句匹配異常,則在finally子句運行后傳播異常: ~~~ try { breedMoreLlamas(); } finally { // Always clean up, even if an exception is thrown. cleanLlamaStalls(); } ~~~ finally子句在所有匹配到的catch子句之后運行: ~~~ try { breedMoreLlamas(); } catch (e) { print('Error: $e'); // Handle the exception first. } finally { cleanLlamaStalls(); // Then clean up. } ~~~ 想了解更多請閱讀[Exceptions]章節
                  <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>

                              哎呀哎呀视频在线观看