CKTIS / PHP
ValueError
Indica um tipo correto mas um valor inadmissível.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
catch (ValueError $failure) { /* handle invalid value */ }Indica um tipo correto mas um valor inadmissível. Uma função interna pode lançar ValueError por intervalo ou opção proibidos e TypeError por tipo incompatível. O código da aplicação também pode usar ValueError para as suas restrições.
try { str_repeat("x",-1); } catch(ValueError $e){$bad=get_class($e);}
try { str_repeat([],2); } catch(TypeError $e){$wrong=get_class($e);}
echo json_encode([str_repeat("x",2),$bad,$wrong]);
["xx","ValueError","TypeError"]
Exemplos
<?php
try { str_repeat("x",-1); } catch(ValueError $e){$bad=get_class($e);}
try { str_repeat([],2); } catch(TypeError $e){$wrong=get_class($e);}
echo json_encode([str_repeat("x",2),$bad,$wrong]);
Resultado
["xx","ValueError","TypeError"]