CKTIS / PHP
RuntimeException
RuntimeException décrit un échec découvert à l’exécution, par exemple une fin de flux imprévue.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
throw new RuntimeException("missing header")RuntimeException décrit un échec découvert à l’exécution, par exemple une fin de flux imprévue. Elle dérive de Exception et peut porter message, code et Throwable précédent. Une fonction qui renvoie false ne crée pas automatiquement cette exception. L’exemple traduit la fin du flux et préserve une ligne vide valide.
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
Exemples
<?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);
Résultat
"" 7 missing header