【发布时间】:2019-06-14 20:04:37
【问题描述】:
for 循环中的 Try-catch 块。第一次尝试,异常进入循环内的 catch 块,在 catch 块内继续将其抛出回循环的开头。但是第二次在 try 块中遇到异常时,它会在调用方法时抛出 try-catch 块,而不是停留在循环中
for (int count = 0; count < listCount; count++)
{
try
{
var selectElement = new SelectElement(pageElement);
selectElement.SelectByText(pagevalue);
break;
}
catch (NoSuchElementException NSE)
{
driver.FindElement(By.Id("policyNumber")).SendKeys(policies[count]);
driver.FindElement(By.Id("btnOK")).Click();
continue;
}
}
希望异常总是在方法内的 try-catch 块内捕获,而不是跳出到方法外的 try-catch。
【问题讨论】:
-
可能抛出的异常不是
NoSuchElementException异常 -
你是对的。没注意到。该元素已经过时并引发了不同的异常。谢谢 dcg