php:捕获异常并继续执行,可能吗?
2022-08-30 07:59:30
是否可以捕获异常并继续执行脚本?
是的,但这取决于您要执行的操作:
例如:
try {
a();
b();
}
catch(Exception $ignored){
}
c();
c()
将始终执行。但如果抛出异常,则不执行。a()
b()
只把东西放到相互依赖的块里。例如: 这取决于一些结果,把它放在块之后是没有意义的。try
b
a
b
try-catch
当然,只需捕获要继续执行的异常...
try
{
SomeOperation();
}
catch (SomeException $ignored)
{
// do nothing... php will ignore and continue
// but maybe use "ignored" as name to silence IDE warnings.
}
当然,这存在默默删除可能是一个非常重要的错误的问题。SomeOperation() 可能会失败,从而导致其他微妙的、难以解决的问题,但你永远不会知道你是否默默地放弃了异常。