Python 3.11+tomllibexception
tomllib.TOMLDecodeError
exceptions
The exception tomllib raises for invalid TOML; a subclass of ValueError whose message names the line and column.
Syntax
except tomllib.TOMLDecodeError as e: ...
Because it is a ValueError, a generic except ValueError catches it too. The lineno, colno, pos and msg attributes exist only from Python 3.14; older versions report the location only in the message text. A common cause is an unquoted string: name = Alice is an error, not a string.
Examples
import tomllib
try:
tomllib.loads('name = Alice')
except tomllib.TOMLDecodeError as e:
print(e) # → Invalid value (at line 1, column 8)
print(isinstance(e, ValueError)) # → True
# Python 3.14+
import tomllib
try:
tomllib.loads('a = 1\nb = ')
except tomllib.TOMLDecodeError as e:
print(e.lineno, e.colno) # → 2 5
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy