CKTIS / PHP
Keyword catch
Handle a thrown object whose class matches the first applicable catch clause.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
catch (RuntimeException|LogicException $error) { /* handle */ }Handle a thrown object whose class matches the first applicable catch clause. A union handles several types with the same block. More specific catches belong before broader ones. Since PHP 8 the variable can be omitted when the object is not needed.
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
outside
Examples
<?php
try {throw new DomainException("outside");}
catch (DomainException|RangeException $e) {echo $e->getMessage();}
catch (Throwable) {echo "other";}
Result
outside