In this video we take a look at a new feature in PHP 7.1 where we can now handle multiple, similar exceptions without duplicating a bunch of catch logic.

To quickly recap, a try / catch block allows us as developers to carry out (try) actions which may potentially fail.

If things do not go according to plan, we can throw an exception.

And then we can gracefully handle (catch) the exception and process the outcome, without letting our program crash.

Prior to PHP 7.1, we would often encounter issues with duplication in our catch blocks should we need to catch multiple potential exceptions.

A good example of this would be in working with a HTTP client library such as Guzzle. There are so many potential pitfalls with making a web request:

  • our server's network connectivity may be down
  • their server's network connectivity may be down
  • the request may be good, but the given credentials are be invalid
  • the request is bad
  • the request is good, but the response didn't contain the expected data

And so on.

Often this would lead to try / catch blocks that looked like:

<?php

try {
   // some client request
catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
   // recover + log with monolog
}
catch (Guzzle\Http\Exception\ServerErrorResponseException $e) {
   // recover + log with monolog
}
catch (Guzzle\Http\Exception\BadResponseException $e) {
   // recover + log with monolog
}
catch(\Exception $e){
   // recover + slightly different log with monolog
}

Plenty of duplication going on there.

And worse, this is often repeated for each API request, of which there can be many on a large scale project.

So PHP 7.1 has us covered here. We can remove most of this logic, and replace thusly:

<?php

try {
   // some client request
catch (Guzzle\Http\Exception\ClientErrorResponseException |
       Guzzle\Http\Exception\ServerErrorResponseException |
       Guzzle\Http\Exception\BadResponseException $e) {
   // recover + log with monolog
}
catch(\Exception $e){
   // recover + slightly different log with monolog
}

Hey, I never said it would be pretty ;) Maybe a nice use statement could tidy this up.

The key here is in the use of the pipe (|) to catch multiple exceptions in a single statement.

Remember here, there is only one variable - at the end of the last exception.

Anyway, this is definitely one of the most immediately useful features of PHP 7.1 in my humble opinion.

The only downside to this is that without a decent set of tests, it could be incredibly scary to refactor. No one will thank you for bodging the exception handling logic of your codebase, and removing all this duplication has no directly tangible business benefit.

But we all write tests, right?


Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.


Episodes in this series

# Title Duration
1 Shorthand Destructuring 06:13
2 Nullable Types 06:37
3 Class Constant Visibility 03:15
4 Void Functions 01:53
5 Multi Catch Exception Handling 05:10