-1

I'm a python beginner and was trying to format the errors using try/except error handling which works as I expect for almost all except for IndentationError

The code that I am trying:

#!/usr/bin/python3

x=1
try:
print(y)
except IndentationError as err:
    print ("You hit",err," !")

When I execute the script, instead of the output of the print statement, I get the standard error:

thinking-machine:~/python-scripts$ ./errHandle.py 
  File "./errHandle.py", line 5
    print(y)
    ^
IndentationError: expected an indented block

As opposed to, say catching NameError:

#!/usr/bin/python3

x=1
try:
    print(y)
except NameError as err:
    print ("You hit",err," !")
thinking-machine:~/python-scripts$ ./errHandle.py 
You hit name 'y' is not defined  !
Freddy Mcloughlan
  • 2,481
  • 1
  • 6
  • 23

0 Answers0