<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國際加速解決方案。 廣告
                # 如何:在查詢表達式中處理異常(C# 編程指南) 可以在查詢表達式的上下文中調用任何方法。但是,建議您避免在查詢表達式中調用任何可能產生副作用(例如,修改數據源的內容或引發異常)的方法。此示例演示在查詢表達式中調用方法時如何避免引發異常,同時又不違反 .NET Framework 的有關異常處理的一般性準則。這些準則說明:如果您了解為什么在給定上下文中會引發特定異常,那么就可以捕捉該異常。有關更多信息,請參見[異常的最佳做法](https://msdn.microsoft.com/zh-cn/library/seyhszts.aspx)。 最后一個示例演示如何處理那些必須在查詢執行過程中引發異常的情況。 下面的示例演示如何將異常處理代碼移至查詢表達式外部。僅當該方法不依賴于查詢的任何本地變量時,才能這樣做。 ``` class ExceptionsOutsideQuery { static void Main() { // DO THIS with a datasource that might // throw an exception. It is easier to deal with // outside of the query expression. IEnumerable<int> dataSource; try { dataSource = GetData(); } catch (InvalidOperationException) { // Handle (or don't handle) the exception // in the way that is appropriate for your application. Console.WriteLine("Invalid operation"); goto Exit; } // If we get here, it is safe to proceed. var query = from i in dataSource select i * i; foreach (var i in query) Console.WriteLine(i.ToString()); //Keep the console window open in debug mode Exit: Console.WriteLine("Press any key to exit"); Console.ReadKey(); } // A data source that is very likely to throw an exception! static IEnumerable<int> GetData() { throw new InvalidOperationException(); } } ``` 在某些情況下,對在查詢內引發的異常的最佳響應可能是立即停止執行查詢。下面的示例演示如何處理可能從查詢正文內部引發的異常。假定 SomeMethodThatMightThrow 可能導致要求停止執行查詢的異常。 請注意,**try** 塊將 **foreach** 循環而不是查詢本身封閉起來。這是因為 **foreach** 循環是實際執行查詢的場所。有關更多信息,請參見 [Introduction to LINQ Queries (C#)](https://msdn.microsoft.com/zh-cn/library/bb397906.aspx)。 ``` class QueryThatThrows { static void Main() { // Data source. string[] files = { "fileA.txt", "fileB.txt", "fileC.txt" }; // Demonstration query that throws. var exceptionDemoQuery = from file in files let n = SomeMethodThatMightThrow(file) select n; // Runtime exceptions are thrown when query is executed. // Therefore they must be handled in the foreach loop. try { foreach (var item in exceptionDemoQuery) { Console.WriteLine("Processing {0}", item); } } // Catch whatever exception you expect to raise // and/or do any necessary cleanup in a finally block catch (InvalidOperationException e) { Console.WriteLine(e.Message); } //Keep the console window open in debug mode Console.WriteLine("Press any key to exit"); Console.ReadKey(); } // Not very useful as a general purpose method. static string SomeMethodThatMightThrow(string s) { if (s[4] == 'C') throw new InvalidOperationException(); return @"C:\newFolder\" + s; } } /* Output: Processing C:\newFolder\fileA.txt Processing C:\newFolder\fileB.txt Operation is not valid due to the current state of the object. */ ``` ## 編譯代碼 * 創建面向 .NET Framework 3.5 版的 Visual Studio 項目。默認情況下,該項目具有一個對 System.Core.dll 的引用以及一條針對 System.Linq 命名空間的 **using** 指令。 * 將代碼復制到項目中。 * 按 F5 編譯并運行程序。 按任意鍵退出控制臺窗口。 ## 請參閱 [LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.aspx)
                  <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>

                              哎呀哎呀视频在线观看