![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How the Non-Capturing Exception Catches work in PHP 8?
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.
Advertisements