How to stop button event when exception fired..?I used try catch blocks, if exception fired it will try to execute next code, i dont want that...pls check comments in code..M i using try catch blocks wrongly..?
here is my code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
driver.FindElement(By.CssSelector(".username")).SendKeys("abc");
driver.FindElement(By.CssSelector(".password")).SendKeys("abcpassword");
driver.FindElement(By.XPath("//a[2]/img")).Click();
}
catch(Exception ex)
{
driver.Quit();
MessegeBox.Show("Log in failed");
//stop button event here only if exception fired
}
try
{
new SelectElement(driver.FindElement(By.Id("CId"))).SelectByText("CVB");
driver.FindElement(By.Name("ddd")).Click();
}
catch(Exception ex)
{
driver.Quit();
MessegeBox.Show("Log in failed");
//stop button event here only if exception fired
}
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlApp.DisplayAlerts = false;
object misValue = System.Reflection.Missing.Value;
}
The
catch
only breaks execution of code insidetry
block. It doesn't return from the method. Typical scenario is indeed just onetry/catch
block in the method (so thatcatch
will be also the last in your method -> method will ends):In your case you have to exit method to avoid other
try/catch
to run, means usereturn
keyword like this: