A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.amitmerchant.com/non-capturing-exception-catches-php8/ below:

Non-capturing exception catches in PHP 8 — Amit Merchant — A blog on PHP, JavaScript, and more

The usual way of handling the exception is by requiring the catch block to catch the exception (thrown from the try block) to a variable like so.

public function bar()
{
    try {
        throw new Exception('foo!');
    } catch (Exception $e) {
        return $e->getMessage();
    } 
}

Here, the exception is being catched in the catch block to a variable $e. This variable now holds any information regarding the exception such as exception message, code, trace, and so on. This is useful in logging exception information to log files or to external services.

But, in several situations, you don’t need information regarding the exception. For instance, if you just want to send a predefined email to the administrator without the need of knowing how the exception has been occurred.

Introducing non-capturing catches

For certain scenario, PHP 8 is introducing “non-capturing catches”. According to this RFC, it can be possible to catch exceptions without capturing them to variables like so.

try {
    throw new Exception('foo!');
} catch (Exception) {
    // send a predefined email to the administrator 
    // irrespective of the exception information
} 

As you can see, the exception variable is completely omitted as the exception details are become irrelevant now.

Thanks for reading! 🚀

More in PHP 8


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3