CKTIS / PHP
Mot-clé try
Encadre des opérations avec au moins un bloc catch ou finally.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
try { process(); } catch (Throwable $e) { recover($e); }Encadre des opérations avec au moins un bloc catch ou finally. Si une opération lève une exception, le reste du try est ignoré et le premier catch compatible s’exécute. Sans catch compatible l’exception se propage après le finally associé.
$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);
["start","caught","cleanup"]
Exemples
<?php
$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);
Résultat
["start","caught","cleanup"]