2 回答

TA貢獻(xiàn)1934條經(jīng)驗 獲得超2個贊
如果我正確理解你的問題,你想要的東西類似于 if-else 但有例外,
一般來說,異常的“if-else”是“try-catch”。也就是下面的代碼片段
try{
this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
} catch(Exception e){
// Do something if any exception is thrown
}
將執(zhí)行try中的內(nèi)容,如果拋出任何異常(在 try 中),將執(zhí)行catch中的代碼。
對于特定的異常,您還可以指定該異常(假設(shè)您已經(jīng)導(dǎo)入了該異常),如下所示
try{
this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
} catch(SessionNotCreatedException e){
// Do something if SessionNotCreatedException is thrown
}

TA貢獻(xiàn)2051條經(jīng)驗 獲得超10個贊
單獨捕獲異常
@BeforeClass
public void setup()throws Exception{
try {
String kobitonServerUrl = "https://f:a15e3b93-a1dd3c-4736-bdfb-
006221ezz8c2a2cz@api.kobiton.com/wd/hub";
this.driver = new RemoteWebDriver (config.kobitonServerUrl(),
config.desireCapabilitites_iphone8());
}
catch (SessionNotCreatedException e){
this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )
}
// if you want to use if else
catch (Exception other){
if ( other.getMessage().contains("SessionNotCreatedException ") )
{
// do something
}
}
}
添加回答
舉報