First hands that fit
Three kinds of exception meet a multi-catch and a general catch. Then a wide catch goes first.
<?phpclass NotFound extends RuntimeException {}class Forbidden extends RuntimeException {}function fetch(string $id): void { if ($id === 'a') throw new NotFound("no $id"); if ($id === 'b') throw new Forbidden("deny $id"); throw new LogicException("bad $id");}foreach (['a', 'b', 'c'] as $id) { try { fetch($id); } catch (NotFound | Forbidden $e) { echo get_class($e), ": ", $e->getMessage(), "\n"; } catch (Exception $e) { echo "general: ", $e->getMessage(), "\n"; }}try { fetch('a');} catch (RuntimeException $e) { echo "wide catch first\n";} catch (NotFound $e) { echo "never reached\n";}1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…
- 4…