1

Hi i'm trying to log indentation error in function test().but when i run the code it's executing without any error. can anyone please tell me the reason here.

def test():
        try:                        //1
                        print "ABC" //2//indentaton error//
                logger.info("printed")//3
        #except Exception as e://
                #logger.error("Exception occured while order dictionary",exc_info=True)
        except IndentationError as e://4
                logger.error("indentation err",exc_info=True)//5
0

3 Answers 3

1

Python parses the entire code file before it begins to execute any code. Unfortunately IndentationError is a subclass of SyntaxError, which is an error that can only be caught from eval, exec, or import statements.

For the purposes of your test, you can write some broken code into a separate file and try to import that file as a module. That way you can catch the IndentationError, if that's relevant to you.

Sign up to request clarification or add additional context in comments.

2 Comments

i have one running code where some indentation changes happened & code was not giving desired output. After spending some time i came to realize it's due to indentation only. I have logger applied in the code but it doesnt log any error.
Yes, that's what the answer says. You cannot use logging inside the file that has wrong indentation, because the wrong indentation is a problem before any code in that file can run. You must use some other means for logging. But you do not need or want logging here, because the error message already tells you where the problem is. Logging is for understanding problems that happened because of input to the program. It is not for finding syntax problems.
0

Consider Python to be comprehending code in levels, where indentations define what level each code is a part of.

As a result, it is possible to go from Level 2 (try statement - line 2) to Level 4 (line 3) just like you can go from Level 2 to Level 3 normally.

However, if you try to go from Level 2 to Level 1 without the try statement being compiled as per its defined syntax, you shall receive an error.

Instead of doing the above to catch indentation errors like above, perhaps, try defining a function that checks for levels instead of errors being thrown by Python compiler.

Just to provide a bit of clarification, here's your code with level and line numbers printed:

1    def test():                                           //Level 1
2            try:                                          //Level 2
3                            print "ABC" //indentaton error//Level 4
4                    logger.info("printed")                //Level 3
5            #except Exception as e:
6                    #logger.error("Exception occured while order dictionary",exc_info=True)
7            except IndentationError as e:                 //Level 2
8                    logger.error("indentation err",exc_info=True)//Level 3

Let me know if you have any other queries.

2 Comments

can you please explain in reference with line no. i'm bit confused with your levels numbering. i have edited the code with line numbers.
Sure, I just added in a bit of clarification with level and line numbers. Let me know if that helps.
0

As alex told, we can follow the same, a one of the workign method is that one. However to catch the syntax error, import the try2.py as module in another file.

try1.py and try2.py are python files respectively.

# try2.py
try:
   import try1
except IndentationError as in_ex:
   print in_ex

#try1.py
def test():
    try:                        
             print "ABC" //2//indentaton error
             logger.info("printed")//3
    #except Exception as e://
            #logger.error("Exception occured while order 
              dictionary",exc_info=True)
    except IndentationError as e://4
            logger.error("indentation err",exc_info=True)//5

1 Comment

what is try1 & try2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.