0

When I try to handle the IndentatioError in except part in following code..

try:
   def Test():
   print("Test Function")
except IndentationError:
   print("Exception raised")

I got this error in output:

File "C:/Users/PycharmProjects/py1/Exception.py", line 3
    print("Test Function")
        ^
IndentationError: expected an indented block

Process finished with exit code 1

I accept the the except part in output.

I want to know how can I handle such type of exception.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • you don`t handle it, you should fix your code formatting ... see https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level?noredirect=1&lq=1 – second Jul 11 '19 at 11:57

4 Answers4

2

You can't.

IndentationError is raised at ""compile"" time. try-except can only handle exceptions that are raised at run time.

What is even the point? IndentationError is originated from badly formatted code. It's not like you can re-format the code in the except block, re-compile it and re-execute it (well you can, in theory, it's just very complex).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

Before executing program compiler checks whether your code has proper indentation, and if not - raises IndentationError. So in general you get IndentationError before you get to except part of the code during code validation stage, not the execution part. So the way to fix it is simply to make proper indentation as follows:

try:
   def Test():
       print("Test Function") //here's the missing tab, that compiler points to
except IndentationError:
   print("Exception raised")

To conclude the above - in case of IndentationError the try-except construction in useless.

Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47
0

You can't except an indentation error in the way, because it is raised when the code is parsed, not when it is run. A try..except block is a runtime construct that doesn't exists at that point in time, so it can't do anything yet.

If you're just interested in catching that exception for curiositie's sake, you can achieve that by writing your code in a way that the exception handling is in place when the raising code is compiled explicitly:

>>> try:
...     compile("""def foo():\nprint('bar')""", '<string>', 'exec')
... except IndentationError:
...     print('baz')
...     
baz

As DeepSpace has also already said, there is little sense in excepting such an error because there is no sane way to fix that kind of program state during runtime.

Arne
  • 17,706
  • 5
  • 83
  • 99
0

An IndentationError occurs any time the parser finds source code that does not follow indentation rules. We can catch it when importing a module. Since the module will be compiled on first import. You can't catch it in the same module that contains the try/except block, because with this exception, Python won't be able to finish compiling the module, and no code in the module will be run.

The issue is with the code indentation. This stops the execution of the python script because it is in the same module.

try:
   def Test():
      print("Test Function")
except IndentationError:
   print("Exception raised")
Zain Aftab
  • 703
  • 7
  • 21
  • "This is no exception but an error." That's not really correct, `IndentationError` is a subclass of `Exception`, and it can certainly be caught in a an except block during runtime. The problem in their example is that they don't make it to runtime. – Arne Jul 11 '19 at 12:18
  • @Arne update the answer. You can go through [here](https://www.tutorialspoint.com/How-to-catch-IndentationError-Exception-in-python) – Zain Aftab Jul 11 '19 at 12:22