<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 功能強大 支持多語言、二開方便! 廣告
                # try-finally(C# 參考) 使用 **finally** 塊,可以清理在 [Try](https://msdn.microsoft.com/zh-CN/library/0yd65esw.aspx) 中分配的任何資源,而且,即使在 **try** 塊中發生異常,您也可以運行代碼。 通常,控件離開 **try** 語句之后,**finally** 的語句會阻止運行。 正常執行中 **break**、**continue**、**goto** 或 **return** 語句的執行,或對 **try** 語句外部異常的傳播,可能會導致發生控件轉換。 已處理的異常中會確保運行關聯的 **finally** 塊。 但是,如果異常未得到處理,則 **finally** 塊的執行取決于如何觸發異常展開操作。 此操作又取決于計算機是如何設置的。 有關更多信息,請參見 [Unhandled Exception Processing in the CLR](http://go.microsoft.com/fwlink/?LinkId=128371)(CLR 中的未經處理的異常處理)。 通常,當未經處理的異常中止應用程序時,**finally** 塊是否運行并不重要。 但是,如果您擁有的 **finally** 塊中的語句必須在該環境下運行,則一個解決方案是將 **catch** 塊添加到 **try**-**finally** 語句中。 或者,可以捕獲可能是在調用堆棧更上方的 **try**-**finally** 語句的 **try** 塊中引發的異常。 即可以捕獲調用了包含 **try**-**finally** 語句的方法中的、或調用了該方法的方法中的、或調用堆棧中任何方法中的異常。 如果未捕獲異常,則 **finally** 塊的執行取決于操作系統是否選擇觸發異常展開操作。 ## 示例 在下面的示例中,無效轉換語句導致 System.InvalidCastException 異常。 異常未處理。 ``` public class ThrowTestA { static void Main() { int i = 123; string s = "Some string"; object obj = s; try { // Invalid conversion; obj contains a string, not a numeric type. i = (int)obj; // The following statement is not run. Console.WriteLine("WriteLine at the end of the try block."); } finally { // To run the program in Visual Studio, type CTRL+F5\. Then // click Cancel in the error dialog. Console.WriteLine("\nExecution of the finally block after an unhandled\n" + "error depends on how the exception unwind operation is triggered."); Console.WriteLine("i = {0}", i); } } // Output: // Unhandled Exception: System.InvalidCastException: Specified cast is not valid. // // Execution of the finally block after an unhandled // error depends on how the exception unwind operation is triggered. // i = 123 } ``` 在下面的示例中,TryCast 方法中的異常在延伸調用堆棧的方法中被捕獲。 ``` public class ThrowTestB { static void Main() { try { // TryCast produces an unhandled exception. TryCast(); } catch (Exception ex) { // Catch the exception that is unhandled in TryCast. Console.WriteLine ("Catching the {0} exception triggers the finally block.", ex.GetType()); // Restore the original unhandled exception. You might not // know what exception to expect, or how to handle it, so pass // it on. throw; } } public static void TryCast() { int i = 123; string s = "Some string"; object obj = s; try { // Invalid conversion; obj contains a string, not a numeric type. i = (int)obj; // The following statement is not run. Console.WriteLine("WriteLine at the end of the try block."); } finally { // Report that the finally block is run, and show that the value of // i has not been changed. Console.WriteLine("\nIn the finally block in TryCast, i = {0}.\n", i); } } // Output: // In the finally block in TryCast, i = 123. // Catching the System.InvalidCastException exception triggers the finally block. // Unhandled Exception: System.InvalidCastException: Specified cast is not valid. } ``` 有關 **finally** 的更多信息,請參見 [try-catch-finally](https://msdn.microsoft.com/zh-CN/library/dszsf989.aspx)。 C# 還包含[使用語句](https://msdn.microsoft.com/zh-CN/library/yh598w02.aspx),該語句可為 [IDisposable](https://msdn.microsoft.com/zh-CN/library/system.idisposable.aspx) 對象提供類似功能的方法語法。 ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-CN/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [C# 參考](https://msdn.microsoft.com/zh-CN/library/618ayhy6.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-CN/library/67ef8sbd.aspx) [C# 關鍵字](https://msdn.microsoft.com/zh-CN/library/x53a06bb.aspx) [try、throw 和 catch 語句 (C++)](https://msdn.microsoft.com/zh-CN/library/6dekhbbc.aspx) [異常處理語句(C# 參考)](https://msdn.microsoft.com/zh-CN/library/s7fekhdy.aspx) [throw(C# 參考)](https://msdn.microsoft.com/zh-CN/library/1ah5wsex.aspx) [try-catch(C# 參考)](https://msdn.microsoft.com/zh-CN/library/0yd65esw.aspx) [如何:顯式引發異常](https://msdn.microsoft.com/zh-CN/library/xhcbs8fz.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>

                              哎呀哎呀视频在线观看