Home > Mobile >  nested try catch catching the same exception
nested try catch catching the same exception

Time:12-29

Is it possible in nested try and catch to catch the same exception in all blocks?

For example:

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

For such scenario the result would be "inner catch fires outer catch fires"

CodePudding user response:

Yes you can do that by throwing an exception from inner catch. Such as:

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

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