PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] nested try catch catching the same exception

 December 29, 2021     exception, laravel, nested, php, try-catch     No comments   

Issue

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 "the inner catch fires, so the outer catch fires, too"


Solution

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';
}


Answered By - Ben
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing