Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

Parola chiave try

Racchiude operazioni con almeno un blocco catch o finally.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

try { process(); } catch (Throwable $e) { recover($e); }

Racchiude operazioni con almeno un blocco catch o finally. Se un’operazione lancia un’eccezione, il resto del try viene saltato e si esegue il primo catch compatibile. Senza catch compatibile l’eccezione si propaga dopo il finally associato.

$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);
["start","caught","cleanup"]

Esempi

<?php
$steps=[]; try {$steps[]="start";throw new RuntimeException();$steps[]="end";}catch(RuntimeException){$steps[]="caught";}finally{$steps[]="cleanup";} echo json_encode($steps);

Risultato

["start","caught","cleanup"]

Fonti