Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword try

Enclose operations in an exception-handling region with at least a catch or a finally block.

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

Syntax

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

Enclose operations in an exception-handling region with at least a catch or a finally block. When an operation throws, remaining statements in the try are skipped, and the first matching catch runs. With no matching catch the throwable propagates after any associated finally.

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

Examples

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

Result

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

Sources