CKTIS / PHP
RuntimeException
RuntimeException descreve uma falha descoberta em execução, como um fim de fluxo inesperado.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
throw new RuntimeException("missing header")RuntimeException descreve uma falha descoberta em execução, como um fim de fluxo inesperado. Deriva de Exception e pode conter mensagem, código e Throwable anterior. Uma função que devolve false não cria automaticamente a exceção. O exemplo traduz o fim do fluxo e conserva uma linha vazia 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
Exemplos
<?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