2016-10-04 111 views
-1

我試圖捕捉System.Windows.Automation.ElementNotAvailableException,但是當異常發生時,Visual Studio仍然拋出它。爲什麼以及如何解決這個問題? 我擡頭爲System.Windows.Automation.ElementNotAvailableException上例外設置(Ctrl鍵 + 上VS 2015 commnunity Alt鍵 + d + È)和未檢查出的類型。try-catch塊內的異常拋出

的try-catch塊是這樣的:

try 
{ 
    appElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, condition); 
} 
catch(ElementNotAvailableException) 
{ 
    appElement = null; 
} 
+1

當你按下會發生什麼。 F5,它應該去catch塊並繼續正常的程序執行。 – Habib

+0

爲什麼你不這樣做,而不是'catch(ElementNotAvailableException ex){MessageBox.Show(ex.Message);}' – MethodMan

+0

你確定Exception是type ElementNotAvailableException –

回答

1

我找到了解決方案。我去工具 - >選項 - >調試 - >常規 - (僅託管)>選中「休息的時候交的AppDomain或管理/本地邊界選項

和它的工作如預期

0

評論這個職位上的異常處理。

How using try catch for exception handling is best practice

另外,還要考慮當前的catch塊後添加一個通用的異常catch塊。除了當前catch塊的特定異常錯誤之外,這將捕獲其他錯誤。

catch(Exception ex) 
{ 
    // exception handling and/or display 
}