In the previous versions of PHP, if we wanted to catch an exception, then we needed it to store in a variable to check whether that variable is used or not.
Before PHP 8, to handle the exception catch block, we needed to catch the exception (thrown by the try block) to a variable.
Example: Capturing Exception Catches in PHP
<?php function foo() { try{ throw new Exception('Hello'); } catch (Exception $e) { return $e->getMessage(); } } ?>
Explanation − In the above program, the exception is being caught by the catch block to a variable $e. Now the $e variable can hold any information about the exception as code, message, etc.
PHP 8 introduced non-capturing catches. Now, it is possible to catch exceptions without capturing them to variables. Now we can neglect the variable.
Example: Non- Capturing Exception Catches in PHP 8
<?php try{ throw new Exception('hello'); } catch (Exception) { // $e variable omitted } ?>
Note: In the above program, we are not using $e variable to hold the exception information.