CKTIS / PHP
Parola chiave catch
Gestisce l’oggetto lanciato nella prima clausola catch compatibile.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
catch (RuntimeException|LogicException $error) { /* handle */ }Gestisce l’oggetto lanciato nella prima clausola catch compatibile. Un’unione permette più tipi nello stesso blocco. Collocare i tipi specifici prima dei generali. Da PHP 8 si può omettere la variabile se l’oggetto non serve.
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
outside
Esempi
<?php
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
Risultato
outside