嵌套的 try catch 捕获相同的异常

nested try catch catching the same exception

嵌套的 trycatch 是否可以在所有块中捕获相同的异常?

例如:

try{
   try{
      throw new Exception("exception");
   }
   catch (Exception $exception)
   {
       echo "inner catch fires";
   }
}
catch (Exception $exception)
{
    echo "outer catch fires";
}

对于这种情况,结果将是“内部着火,所以外部也着火”

是的,您可以通过从内部捕获中抛出异常来做到这一点。如:

try {
    try {
        throw new Exception('exception');
    } catch (Exception $exception) {
        echo 'inner catch fires';

        throw $exception;
    }
} catch (Exception $exception) {
    echo 'outer catch fires';
}