PHP 7 interview question · Most asked
What is multi-catch exception handling in PHP 7.1?
Summary
A single catch block can handle several exception types separated by |, e.g. catch (InvalidArgumentException | LengthException $e).
Longer Answer
It removes duplicated catch bodies when different exceptions need identical handling. The caught variable's type is the union; use instanceof if you must distinguish. Order still matters: the first matching block wins.
Code Example
<?php
try {
$client->send($request);
} catch (ConnectException | TimeoutException $e) {
$this->retryLater($request, $e);
}
Key Points
catch (A | B $e)- Removes duplicated handlers
- First matching block wins
More most-asked PHP 7 questions
- What are typed properties in PHP 7.4?
- How do return type declarations work in PHP 7?
- What were the headline improvements of PHP 7.0 over PHP 5.6?
- Explain scalar type declarations and coercive vs strict mode in PHP 7.
- Explain the `Throwable`, `Error` and `Exception` hierarchy introduced in PHP 7.
- What are nullable types in PHP 7.1?
- What does the null coalescing operator `??` do, and how is it different from `isset()` with a ternary?
- What are arrow functions in PHP 7.4 and how do they differ from closures?