[php-src] Issue #9662: Exception thrown in catch is caugh silently when finally has return statement
| From: | tomasfejfar | Date: | Mon, 03 Oct 2022 11:44:48 +0000 |
| Subject: | [php-src] Issue #9662: Exception thrown in catch is caugh silently when finally has return statement | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242514@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9662
Author: tomasfejfar
### Description
The following code:
```php
<?php
define('DO_RETURN', true);
function execute() {
try {
throw new \Exception('root exception'); // throws exception
} catch (\Throwable $e) {
echo "[catch]"; // exception is handled somehow
throw $e; // and rethrown
return; // never called
} finally {
echo "[finally]"; // finally is handed
if (DO_RETURN)
return; // if return is here the thrown exception is ignored and silently scratched
}
}
execute(); // the exception was rethrown, so should be thrown after finally
```
Resulted in this output:
```
[catch][finally]
```
But I expected this output instead:
```
[catch][finally]
Fatal error: Uncaught Exception: root exception in /in/aLqm4:7
Stack trace:
#0 /in/aLqm4(19): execute()
#1 {main}
thrown in /in/aLqm4 on line 7
Process exited with code 255.
```
https://3v4l.org/aLqm4
I understand that it happens because
> Additionally, if the
> [finally](https://www.php.net/manual/en/language.exceptions.php#language.exceptions.finally) block
> also contains a [return](https://www.php.net/manual/en/function.return.php) statement, the value
> from the
> [finally](https://www.php.net/manual/en/language.exceptions.php#language.exceptions.finally) block
> is returned.
-- https://www.php.net/manual/en/language.exceptions.php#language.exceptions.finally
But I don't think it should be possible to silently catch an exception outside of catch
statement.
### PHP Version
7.4.0 - 7.4.32, 8.0.1 - 8.0.24, 8.1.0 - 8.1.11, 8.2rc1 - rc3
### Operating System
any OS