您可以使用多个 catch 处理相同的异常吗
Can you use multiple catch with same exception
public function() {
try {
// execute code and if error jump into 1st catch
return output;
} catch (Exception e){
//execute code and if error jump in 2nd catch
return output1
} catch (Exception e){
//execute code and stop
return output 2
}
}
以上是我想要实现的代码流程,我想检查是否有办法或更好的方法来完成我想尝试一段代码的场景,如果失败,先捕获并执行捕获一段代码,如果它再次失败并且在第一次捕获中发生异常,也会在最后一次捕获中找到最后一段代码。很感谢任何形式的帮助。提前致谢。
这是我想要实现的流程:
try ----if error/exception----> catch1 ----if error/exception----> catch2
你不能那样做。要在 catch 块中捕获异常,您应该使用另一个 try/catch.
public function() {
try {
return output;
} catch (Exception e){
try {
return output1;
} catch (Exception e2) {
return output2;
}
}
}
public function() {
try {
// execute code and if error jump into 1st catch
return output;
} catch (Exception e){
//execute code and if error jump in 2nd catch
return output1
} catch (Exception e){
//execute code and stop
return output 2
}
}
以上是我想要实现的代码流程,我想检查是否有办法或更好的方法来完成我想尝试一段代码的场景,如果失败,先捕获并执行捕获一段代码,如果它再次失败并且在第一次捕获中发生异常,也会在最后一次捕获中找到最后一段代码。很感谢任何形式的帮助。提前致谢。
这是我想要实现的流程: try ----if error/exception----> catch1 ----if error/exception----> catch2
你不能那样做。要在 catch 块中捕获异常,您应该使用另一个 try/catch.
public function() {
try {
return output;
} catch (Exception e){
try {
return output1;
} catch (Exception e2) {
return output2;
}
}
}