CKTIS / PHP
RuntimeException
RuntimeException descrive un errore scoperto in esecuzione, come la fine imprevista di un flusso.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
throw new RuntimeException("missing header")RuntimeException descrive un errore scoperto in esecuzione, come la fine imprevista di un flusso. Deriva da Exception e può contenere messaggio, codice e Throwable precedente. Una funzione che restituisce false non crea automaticamente l’eccezione. L’esempio traduce la fine del flusso e conserva una riga vuota valida.
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
Esempi
<?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);
Risultato
"" 7 missing header