CKTIS / PHP
RuntimeException
RuntimeException opisuje pogrešku otkrivenu pri izvršavanju, primjerice neočekivan kraj toka.
Verzija: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
throw new RuntimeException("missing header")RuntimeException opisuje pogrešku otkrivenu pri izvršavanju, primjerice neočekivan kraj toka. Nasljeđuje Exception i može nositi poruku, kod i prethodni Throwable. Funkcija koja vraća false sama ne stvara tu iznimku. Primjer izričito pretvara kraj toka u pogrešku i čuva valjani prazan redak.
function headerLine($stream): string {
$line = fgets($stream);
if ($line === false) { throw new RuntimeException("missing header", 7); }
return rtrim($line, "\r\n");
}
$f = fopen("php://memory", "w+"); fwrite($f, "\n"); rewind($f);
echo json_encode(headerLine($f)), PHP_EOL;
try { headerLine($f); }
catch (RuntimeException $e) { echo $e->getCode(), " ", $e->getMessage(); }
fclose($f);
""
7 missing header
Primjeri
<?php
function headerLine($stream): string {
$line = fgets($stream);
if ($line === false) { throw new RuntimeException("missing header", 7); }
return rtrim($line, "\r\n");
}
$f = fopen("php://memory", "w+"); fwrite($f, "\n"); rewind($f);
echo json_encode(headerLine($f)), PHP_EOL;
try { headerLine($f); }
catch (RuntimeException $e) { echo $e->getCode(), " ", $e->getMessage(); }
fclose($f);
Rezultat
"" 7 missing header