Exceptions

Published

2023-07-31

Exceptions

ValueError

In an earlier chapter, we saw how trying to use math.sqrt() with a negative number as an argument results in a ValueError.

In this chapter we’ve seen another case of ValueError—where inputs to numeric constructors, int() and float(), are invalid. These functions take strings, so the issue isn’t the type of the argument. Rather, it’s an issue with the value of the argument—some strings cannot be used to construct an object of numeric types.

For example,

>>> int('5')
5
>>> int('5.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.0'
>>> int('kumquat')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'kumquat'

The first call, with the argument '5' succeeds because the string '5' can be used to construct an object of type int. The other two calls fail with a ValueError.

int('5.0') fails because Python doesn’t know what to do about the decimal point when trying to construct an int. Even though 5.0 has a corresponding integer value, 5, Python rejects this input and raises a ValueError.

The last example, int('kumquat'), fails for the obvious reason that the string 'kumquat' cannot be used to construct an integer.

What about the float constructor, float()? Most numeric strings are valid arguments to the float constructor. Examples:

>>> float('3.1415926')
3.1415926
>>> float('7')
7.0
>>> float('6.02E23')
6.02e+23

The first example should be obvious. The second example is OK because '7' can be used to construct a float. Notice the result of float('7') is 7.0. The third example shows that the float constructor works when using scientific notation as well.

Python has special values for positive and negative infinity (these do come in handy from time to time), and the float constructor can handle the following strings:

>>> float('+inf')
inf
>>> float('-inf')
-inf

Here are some examples of conversions that will fail, resulting in ValueError.

>>> float('1,000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1,000'
>>> float('pi')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'pi'
>>> float('one')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'one'
>>> float('toothache')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'toothache'

What about the string constructor, str()? It turns out that this can never fail, since all objects in Python have a default string representation. There’s no object type that can’t be turned into a string! Even things like functions have a default string representation (though this representation isn’t particularly human-friendly). Example:

>>> def f():
...     return 0
... 
>>> str(f)
'<function f at 0x10101ea70>'

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).