2017年7月31日 星期一

[C#] Try Catch Finally issue

  • try-catch
    • 超出一般控制流程的程式碼會處理這些例外狀況
    • throw 
      • 在程式執行期間發出出現例外狀況的訊號
      • 拋出捕抓到的例外
      • Code
                  try
                  {
                      // throw 的訊息會導引至 catch 中顯示
                      if (sName.Equals("Lyndon"))
                          throw new ApplicationException("DemoError");
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Error: {0}", e.Message);
                  }
    • try-catch-finally
      • finally 
        • 選擇性區塊
        • finally 中的程式無論如何都會執行 
        • 通常將釋放資源的程式寫在 finally 中
      • Code
                  try
                  {
                      // try 區塊中的資源、處理
                  }
                  catch (Exception e)
                  {
                      // catch 區塊中的例外情況
                  }
                  finally
                  {
                      // 釋放 try 區塊中所使用的資源
                      // 撰寫 finally 區塊
                      // 1. 程式執行順序最後一定會跑至 finally 區塊
                      // 2. 如 try / catch 區塊中,使用 return,finally 區塊還是會被執行
                  }
  • try-catch block
    • Code
    
    public class TryCatchPerformanceLib
        {
    public int nCount = 1000000;
            public long lSum = 0;
            public bool flag = false;
            public int nDiv = 100000;
            public int nDiv2 = 200000;
    
            /// 
            /// Case1: no try catch
            /// 
            /// Sum number
            public long Case1()
            {
                lSum = 0;
                for (int i = 0; i < nCount; i++)
                {
                    lSum += i;
                    if (i % nDiv == 0)
                        flag = true;
                }
    
                return lSum;
            }
    
            /// 
            /// Case2: try catch
            /// 
            /// Sum number
            public long Case2()
            {
                lSum = 0;
                for (int i = 0; i < nCount; i++)
                {
                    try
                    {
                        lSum += i;
                        if (i % nDiv == 0)
                            flag = true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: {0}", e.Message);
                    }
                }
    
                return lSum;
            }
    
            /// 
            /// Case2: try catch and one exception
            /// 
            /// Sum number
            public long Case3()
            {
                lSum = 0;
                for (int i = 0; i < nCount; i++)
                {
                    try
                    {
                        lSum += i;
                        if (i % nDiv == 0)
                            throw new ApplicationException("DemoError");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: {0}", e.Message);
                    }
                }
    
                return lSum;
            }
    
            /// 
            /// Case2: try catch and one exception (reduce frequency)
            /// 
            /// Sum number
            public long Case4()
            {
                lSum = 0;
                for (int i = 0; i < nCount; i++)
                {
                    try
                    {
                        lSum += i;
                        if (i % nDiv2 == 0)
                            throw new ApplicationException("DemoError");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: {0}", e.Message);
                    }
                }
    
                return lSum;
            }
        }
    • Result
      • Case1 Sum=499,999,500,000 Time=11ms
      • Case2 Sum=499,999,500,000 Time=11ms
      • Case3 Sum=499,999,500,000 Time=121ms
      • Case4 Sum=499,999,500,000 Time=45ms
    1. try…catch 只有在發生 Exception 時才會影響效能
    2. 當 catch 越頻繁影響效能越嚴重
  • Ref:
    • http://blog.darkthread.net/post-2009-12-20-try-catch-performance.aspx
    • http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/11/14/kb-how-avoidable-try-catch-tortures-your-machine.aspx
    • https://dotblogs.com.tw/yc421206/archive/2011/06/09/27445.aspx
    • https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/keywords/exception-handling-statements
    • http://teddy-chen-tw.blogspot.tw/2013/11/javatrycatchfinally1java-se-7.html

沒有留言:

張貼留言