Catch IndentationError Exception in Python



Python uses indentation to define blocks of code instead of curly braces or keywords. If the indentation is not correct, Python raises an IndentationError. In this article, you will learn how to catch and handle IndentationError in Python using various approaches.

An IndentationError in Python occurs when the indentation rules are not followed properly. It is raised during the parsing stage, not during execution. Therefore, to catch it, you must execute the code dynamically as a string. Using exec(), compile(), and custom wrapper functions, you can catch and handle this error in Python programs.

We can use the following methods to catch an IndentationError in Python -

  • Using exec() with a try-except block
  • Using compile() with a try-except block
  • Using a custom function with error handling

Using exec() Function

The exec() function is used to run a string of Python code dynamically. If the code has an indentation problem, an IndentationError will be raised and can be caught inside a try-except block.

Example

In the following example, we pass a string with incorrect indentation to the exec() function. resulting in an IndentationError -

Open Compiler
try: exec("def greet():\nprint('Hello')") except IndentationError: print("IndentationError caught: Improper indentation in the code.")

The output of the above code is shown below -

IndentationError caught: Improper indentation in the code.

Using compile() Function

The compile() function is used to compile a string of code into a code object. If there is an indentation problem, Python raises an IndentationError while compiling.

Example

In this example, we compile a faulty function with missing indentation and catch the error -

Open Compiler
code = "def add():\nreturn 2 + 3" try: compiled = compile(code, "<string>", "exec") exec(compiled) except IndentationError: print("IndentationError caught during compilation.")

We get the output as shown below -

IndentationError caught during compilation.

Using Custom Function with Error Handling

You can write a custom function that receives code as a string and attempts to execute it. If there is any indentation issue, it will catch and print the IndentationError.

Example

In the following example, a function is defined to test code snippets for indentation problems -

Open Compiler
def check_indentation(code_str): try: exec(code_str) except IndentationError as e: print("IndentationError caught:", e) check_indentation("for i in range(3):\nprint(i)")

Following is the output obtained -

IndentationError caught: expected an indented block after 'for' statement on line 1 (<string>, line 2)

Why IndentationError Needs Special Handling

Since IndentationError is a subclass of SyntaxError and occurs before the program runs, it cannot be caught in a normal try-except block if the indentation issue is in the actual code.

It must be detected by running the code dynamically using exec() or compile() functions.

Updated on: 2025-05-27T17:25:30+05:30

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements