Locating the line number where an exception occurs in python code

前端 未结 10 2052
长情又很酷
长情又很酷 2020-12-16 18:55

I have a code similar to this:

try:
  if x:
      statement1
      statement2
      statement3
  elif y:
      statement4
      statement5
      statement6
          


        
10条回答
  •  醉酒成梦
    2020-12-16 18:58

    I've done the following before:

    try:
        doing = "statement1"
        statement1
        doing = "statement2"
        statement2
        doing = "statement3"
        statement3
        doing = "statement4"
        statement4
    
     except:
        print "exception occurred doing ", doing
    

    The advantage over printing checkpoints is there's no log output unless there actually is an exception.

提交回复
热议问题