DEV Community

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

Posted on • Updated on

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

Discussion (0)