all Python 3Exceptionsexception
IndentationError
built-in exceptions
Incorrect indentation (a subclass of SyntaxError).
Syntax
raise IndentationError
Because it subclasses SyntaxError, this is raised while the file is being compiled, before a single line runs. So you cannot wrap your own badly indented code in try/except — the handler only fires where code is compiled at runtime (exec, compile, importing another module). A common special case is TabError, a subclass of IndentationError for mixed tabs and spaces; setting your editor to four spaces makes it go away.
Examples
try:
exec(' x = 1')
except IndentationError as e:
print(type(e).__name__) # → IndentationError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy