Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / PHP

Słowo kluczowe try

Obejmuje operacje obszarem obsługi wyjątków z przynajmniej jednym catch lub finally.

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

Składnia

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

Obejmuje operacje obszarem obsługi wyjątków z przynajmniej jednym catch lub finally. Gdy operacja rzuci wyjątek, reszta bloku try zostaje pominięta i wykonuje się pierwszy pasujący catch. Bez pasującego catch wyjątek idzie dalej po wykonaniu powiązanego finally.

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

Przykłady

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

Wynik

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

Źródła