CKTIS / PHP
RuntimeException
RuntimeException describe un fallo descubierto en ejecución, como un fin de flujo inesperado.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
throw new RuntimeException("missing header")RuntimeException describe un fallo descubierto en ejecución, como un fin de flujo inesperado. Deriva de Exception y puede llevar mensaje, código y Throwable anterior. Una función que devuelve false no crea automáticamente esta excepción. El ejemplo traduce el fin de flujo y conserva una línea vacía válida.
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
Ejemplos
<?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);
Resultado
"" 7 missing header