我想捕獲像這樣的多個異常,但是出現錯誤'); expected'
。我如何使用||
來做到這一點?您使用的是Java 7如何在捕獲多個webdriver異常時進行OR操作
try {
//find an element here
}catch(StaleElementReferenceException e || NoSuchElementException e) {
//do something
}
我想捕獲像這樣的多個異常,但是出現錯誤'); expected'
。我如何使用||
來做到這一點?您使用的是Java 7如何在捕獲多個webdriver異常時進行OR操作
try {
//find an element here
}catch(StaleElementReferenceException e || NoSuchElementException e) {
//do something
}
假設,你應該能夠使用這個語法:
catch (StaleElementReferenceException | NoSuchElementException e)
注意單|
,以及單變量名。
請參閱"Catching Multiple Exception Types And Rethrowing Exceptions With Improved Type Checking"文檔瞭解更多詳情(吸引人的標題,是吧?)。
如果您不使用Java 7,則需要多個catch
塊。
試試這個:
try {
//find an element here
}catch(StaleElementReferenceException e){
// do something
}catch(NoSuchElementException e) {
//do something
}
注:第二catch塊必須捕獲更廣泛的或更新的異常否則代碼將無法編譯。