CKTIS / PHP
ValueError
Indique un type correct mais une valeur inadmissible.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
catch (ValueError $failure) { /* handle invalid value */ }Indique un type correct mais une valeur inadmissible. Une fonction intégrée peut lever ValueError pour une plage ou option interdite, TypeError pour un type incompatible. Le code applicatif peut aussi lever ValueError pour ses contraintes.
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"]
Exemples
<?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]);
Résultat
["xx","ValueError","TypeError"]