我正在尝试捕获这样的多个异常但我收到错误'); expected'
。如何使用||
?
try {
//find an element here
}catch( StaleElementReferenceException e || NoSuchElementException e) {
//do something
}
答案 0 :(得分:5)
假设您使用的是Java 7,您应该可以使用以下语法:
catch (StaleElementReferenceException | NoSuchElementException e)
请注意单 |
以及单变量名称。
有关详细信息,请参阅"Catching Multiple Exception Types And Rethrowing Exceptions With Improved Type Checking" doc(引人注目的标题,是吗?)。
如果您不使用Java 7,则需要多个catch
块。
答案 1 :(得分:0)
试试这个:
try {
//find an element here
}catch( StaleElementReferenceException e){
// do something
}catch(NoSuchElementException e) {
//do something
}
注意:第二个catch块必须捕获更广泛或更新的异常,否则代码将无法编译。