2012-05-15 35 views
0

我正在運行此測試用例,並使用NUNIT並將NUNIT附加到Visual Studio調試器(Debug ---> Attach to Process ---> Select nunit 。可執行程序)。在c#中的try塊中拋出異常 - 通過將nunit附加到VSS調試器進行調試

我在等待語句「拋出新異常(」Exception 1 occured!「);」將控件移入catch塊但是 控件不會跳轉到「catch」,而是跳轉到method2() 。

當我只運行nunit測試用例而不將nunit.exe附加到Visual Studio調試器時,代碼的行爲正常。

public void test() 
{ 
try 
      { 
       method1(); 
       if (condition1) 
       { 
        throw new Exception("Exception 1 occured!"); 
       } 

       method2(); 
       if (condition2) 
       { 
        throw new Exception("Exception 2 occured!"); 
       } 

       method3(); 
       if (condition3) 
       { 
        throw new Exception("Exception 3 occured!"); 
       } 

      } 
      catch (Exception Ex) <---- Exceptions thrown above are caught here 
      { 
       logMessage(E.Message); 
       throw;     
      }     
} 

回答

1

Visual Studio可能設置爲在所有異常中斷而不是僅僅未捕獲的異常。所以當它檢測到你拋出異常時停止。在某些情況下,Visual Studio突出顯示即將運行的下一行代碼,而不是在引發異常的行上突然中斷。

如果您繼續瀏覽代碼,則應該看到它接下來會轉到catch塊。

+0

這是完全正確的。正如你在上面指出的那樣,在我的情況下,Visual Studio突出顯示即將運行的下一行代碼,而不是顯示拋出異常的那一行。當我繼續瀏覽代碼時 - 我確實接觸到catch塊。 – User1