CKTIS / PHP
Ключова дума try
Обхваща операции с поне един блок catch или finally.
Версия: PHP 8.4; repository runtime, production runtime not attested
Синтаксис
try { process(); } catch (Throwable $e) { recover($e); }Обхваща операции с поне един блок catch или finally. Ако операция хвърли изключение, остатъкът от try се пропуска и се изпълнява първият съвместим catch. Без такъв catch изключението се разпространява след съответния finally.
$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);
["start","caught","cleanup"]
Примери
<?php
$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);
Резултат
["start","caught","cleanup"]