CKTIS / PHP
ValueError
Označi pravilen tip, vendar nedovoljeno vrednost.
Različica: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
catch (ValueError $failure) { /* handle invalid value */ }Označi pravilen tip, vendar nedovoljeno vrednost. Vgrajena funkcija lahko sproži ValueError za prepovedan razpon ali možnost in TypeError za nezdružljiv tip. Tudi aplikacija lahko uporabi ValueError za svoje omejitve.
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"]
Primeri
<?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]);
Rezultat
["xx","ValueError","TypeError"]