Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

ValueError

Indica un tipo correcto pero un valor inadmisible.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

catch (ValueError $failure) { /* handle invalid value */ }

Indica un tipo correcto pero un valor inadmisible. Una función integrada puede lanzar ValueError por rango u opción prohibidos y TypeError por tipo incompatible. El código de aplicación también puede usar ValueError para sus propias restricciones.

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"]

Ejemplos

<?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"]

Fuentes