ValueError
What is ValueError?
ValueError: A value has the right type but an inappropriate or invalid value for the operation being performed. A ValueError in Python means a function received a value of the right type but with an unacceptable value. int("abc") raises it, because "abc" is a string, as expected, but not one that represents a number.
Frequently asked questions about ValueError
How is ValueError used in practice?
PHP has a similar ValueError for built-in functions, and frameworks often use InvalidArgumentException for the same idea in your own code.
What should you keep in mind with ValueError?
Raise this kind of error in your own functions when input makes no sense, such as a negative quantity or an end date before the start date. Failing early with a clear message is far better than continuing with bad data. For user input, validate before calling such functions and show friendly messages, so a ValueError signals a programming mistake rather than a typo in a form.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |