Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sameer Borate:
Handling multiple exceptions in PHP 7.1
Feb 14, 2017 @ 15:40:16

Sameer Borate has a new post to his site today showing how you can handle multiple exceptions in PHP 7.1, the latest release in the PHP 7.x series.

Exception handling is one of the ways in which modern languages handle errors. PHP usually handles exception in a separate catch block for each different type of exception. Below is a example exception code that uses three custom exceptions ‘MyFooException’, ‘MyBarException’ and ‘MyBazException’ that is later handled in a ‘try’ block.

He includes a bit of code showing the "old way" of doing it with multiple catch statements, one for each type of exception. With PHP 7.1 however, you can collapse all of those down into a single catch statement and type hint each type you want caught (pipe delimited). This can make for much cleaner code and a lot less repetition in your error/exception handling.

tagged: multiple exception php71 try catch single tutorial

Link: http://www.codediesel.com/php/handling-multiple-exceptions-in-php-7-1/


Trending Topics: