CKTIS / PHP
Sleutelwoord catch
Vangt het geworpen object op in de eerste passende catch-clausule.
Versie: PHP 8.4; repository runtime, production runtime not attested
Syntax
catch (RuntimeException|LogicException $error) { /* handle */ }Vangt het geworpen object op in de eerste passende catch-clausule. Een unie staat meerdere typen in één blok toe. Zet specifieke typen vóór algemene. Sinds PHP 8 mag de variabele wegblijven wanneer het object niet nodig is.
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
outside
Voorbeelden
<?php
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
Resultaat
outside