我们最近遇到了一个Java服务器应用程序的问题,其中应用程序抛出了未捕获的错误,因为Error是Throwable的一个单独的子类,我们只捕获异常.
我们通过捕获Throwables而不是Exceptions解决了当前的问题,但是这让我想到为什么你想要捕获Exceptions而不是Throwables,因为你会错过错误.
所以,当你能捕获Throwables时,你为什么要捕捉异常呢?
我有一个关于"元素不再附加到DOM"的问题.
我尝试了不同的解决方案,但他们间歇性地工作.请建议一个永久性的解决方案.
WebElement getStaleElemById(String id, WebDriver driver) {
try {
return driver.findElement(By.id(id));
} catch (StaleElementReferenceException e) {
System.out.println("Attempting to recover from StaleElementReferenceException ...");
return getStaleElemById(id, driver);
}
}
WebElement getStaleElemByCss(String css, WebDriver driver) {
try {
return driver.findElement(By.cssSelector(css));
} catch (StaleElementReferenceException e) {
System.out.println("Attempting to recover from StaleElementReferenceException ...");
return getStaleElemByCss(css, driver);
} catch (NoSuchElementException ele) {
System.out.println("Attempting to recover from NoSuchElementException ...");
return getStaleElemByCss(css, driver);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Anu
API doc说从不捕获Throwable子类Error表示异常行为.这是否意味着Error和Exception之间的隔离是告诉程序员应该捕获哪个子类,哪个不应该?或者还有更多呢?