all Python 3configparserexception
configparser.Error
exceptions
The base exception for every configparser error: missing sections and options, parsing failures and interpolation problems.
Syntax
except configparser.Error as e: ...
except configparser.Error does not catch everything config handling can raise: cfg['missing'] gives KeyError, getint() on 'abc' gives ValueError, and set() with a number gives TypeError. Those three are built-in exceptions and need their own handlers.
Examples
import configparser
cfg = configparser.ConfigParser()
cfg.read_string("[db]\nport = abc\n")
print(issubclass(configparser.NoSectionError, configparser.Error)) # → True
try:
cfg.getint("db", "port")
except configparser.Error:
print("не сюда")
except ValueError as e:
print("ValueError:", e) # → ValueError: invalid literal for int() with base 10: 'abc'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy