CKTIS / PHP
Mot-clé catch
Traite l’objet levé dans la première clause catch compatible.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
catch (RuntimeException|LogicException $error) { /* handle */ }Traite l’objet levé dans la première clause catch compatible. Une union rassemble plusieurs types dans le même bloc. Placez les types précis avant les généraux. Depuis PHP 8 la variable peut être omise si l’objet est inutile.
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
outside
Exemples
<?php
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
Résultat
outside