【问题标题】:nested try catch catching the same exception嵌套的 try catch 捕获相同的异常
【发布时间】:2022-01-27 03:44:15
【问题描述】:

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

例如:

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

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

【问题讨论】:

  • throw 在那个 catch 块中再次出现同样的异常?
  • 你尝试了什么,结果如何?

标签: php laravel exception nested try-catch


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多