Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exception bubbling confusion

Tags:

exception

php

I read that when an exception is thrown inside a class method, execution stops and the exception bubbles up the call stack looking for immediate catch block of the same exception type.

take for example the below code.

class Foo
{

    public function methodOne()
    {
        $this->methodTwo();
        // more code
    }

    public function methodTwo()
    {
        try {
            $this->methodThree();
        } catch (Exception $e) {

        }
    }

    public function methodThree()
    {
        throw new Exception('exception happened');
    }

}

$foo = new Foo();
$foo->methodOne();

My question is, when an exception is caught up the call stack, where does the execution begin again? for example above where the exception is caught in methodTwo, will the execution move down to continue in methodOne which was interupted by exception in methodThree?

or in other words, after the exception is caught, does it preserve the call stack?

like image 988
Cholthi Paul Ttiopic Avatar asked May 09 '26 07:05

Cholthi Paul Ttiopic


1 Answers

I took your code and added backtrace print and code executing so you can see what happens.

The answer to your question would be that program continues execution in catch part of try catch block and goes on to the code after it when done with error.

It looks similar to this (I've improved formatting from the original). You can see the example below.

#0  Foo->methodThree() called at [/php_playground/index.php:25] <br> 
#1 Foo->methodTwo() called at [/php_playground/index.php:16] <br>
#2 Foo->methodOne() called at [/php_playground/index.php:46] <hr>

methodTwo caught error
#0  Foo->methodTwo() called at [/php_playground/index.php:16] <br>
#1  Foo->methodOne() called at[/php_playground/index.php:46] <hr> 

methodTwo continuing execution
#0  Foo->methodTwo() called at [/php_playground/index.php:16] <br>
#1  Foo->methodOne() called at [/php_playground/index.php:46] <hr> 

methodOne continuing execution
#0  Foo->methodOne() called at [/php_playground/index.php:46]

so the answer to your question would be that program continues execution in catch part of try catch block and goes on to the code after it when done with error.

Here is the code I've used for this print so you can play with it yourself.

class Foo
{

    public function methodOne()
    {
        $this->methodTwo();
        echo "<p>methodOne continuing execution</p>";
        debug_print_backtrace();
        echo "<hr>";
    }

    public function methodTwo()
    {
        try {
            $this->methodThree();
        } catch (Exception $e) {
            echo "<p>methodTwo caught error</p>";
            debug_print_backtrace();
            echo "<hr>";
        }

        echo "<p>methodTwo continuing execution</p>";
        debug_print_backtrace();
        echo "<hr>";
    }

    public function methodThree()
    {
        debug_print_backtrace();
        echo "<hr>";
        throw new Exception('exception happened');
    }
}
like image 139
boroboris Avatar answered May 10 '26 21:05

boroboris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!