DEV Community

Cover image for Handle Multiple Exceptions with PHP 7.1
Pierre-Henry Soria ✨
Pierre-Henry Soria ✨

Posted on • Edited on

4 2

Handle Multiple Exceptions with PHP 7.1

Here is how you can handle easily multiple exceptions thanks to PHP 7.1.

👉 Source code of the example project https://gist.github.com/pH-7/5ba7f83a9e1befc46a68379301eec41e

Example

try {
    $person = new Person('Pierre');
    echo $person->greeting('morning'); // can be 'morning', 'afternoon', or 'evening'
} catch(InvalidNameException | InvalidDayMomentException $err) { // Catch multiple exceptions at once
    echo $err->getMessage();
}
Enter fullscreen mode Exit fullscreen mode

👉 My Udemy course on how to build a real-world PHP 8.1 application from scratch - https://www.udemy.com/course/create-real-world-php-webapp-from-scratch/

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay