CKTIS / PHP
ValueError
Označuje správny typ, ale nepovolenú hodnotu.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
catch (ValueError $failure) { /* handle invalid value */ }Označuje správny typ, ale nepovolenú hodnotu. Vstavaná funkcia môže vyvolať ValueError pre zakázaný rozsah či voľbu a TypeError pre nekompatibilný typ. Aj aplikácia môže použiť ValueError pre vlastné obmedzenia.
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"]
Príklady
<?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]);
Výsledok
["xx","ValueError","TypeError"]