Locating the line number where an exception occurs in python code

前端 未结 10 2037
长情又很酷
长情又很酷 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.

    0 讨论(0)
  • 2020-12-16 18:59

    You should run your program in a debugger, such as pdb. This will allow you to run your code normally, and then examine the environment when something unexpected like this occurs.

    Given a script named 'main.py', run it like this:

    python -m pdb main.py
    

    Then, when your program starts, it will start in the debugger. Type c to continue until the next breakpoint (or crash). Then, you can examine the environment by doing things like print spam.eggs. You can also set breakpoints by doing pdb.set_trace() (I commonly do import pdb; pdb.set_trace()).

    Additionally, what do you mean that it is "okay" for 'statement 3' to raise the exception? Are you expecting the exception? If so, it might be better to write a try/except block around this statement, so that the program can continue.

    0 讨论(0)
  • 2020-12-16 18:59

    Building on JJ above..

    The advantage of using system errors over statements is they record more specific information which will aid debugging later (believe me I get a lot)

    eg. I record them to a text file, so after my programs have automatically run overnight on the server, I can retrieve any issues, and have enough information to quicken the repair!

    More Info... Traceback & Sys

    import traceback
    import sys
    
    try:
        print 1/0
    
    except Exception as e:
        print '1', e.__doc__
        print '2', sys.exc_info()
        print '3', sys.exc_info()[0]
        print '4', sys.exc_info()[1]
        print '5', sys.exc_info()[2], 'Sorry I mean line...',traceback.tb_lineno(sys.exc_info()[2])
        ex_type, ex, tb = sys.exc_info()
        print '6', traceback.print_tb(tb)
    

    Yields

    >  1  Second argument to a division or modulo operation was zero. 
    >  2  (<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division
    >      or modulo by zero',), <traceback object at 0x022DCF30>) 
    >  3  <type 'exceptions.ZeroDivisionError'> 
    >  4  integer division or modulo by zero 
    >  5  <traceback object at 0x022DCF30> Sorry I mean line... 5
    >  6  File "Z:\Programming\Python 2.7\Error.py", line 5, in <module>
    >     print 1/0 
          None
    >>>
    
    0 讨论(0)
  • 2020-12-16 19:03

    You should wrap the statements you care about more tightly. Extracting the line number from the traceback is going to be involved and fragile.

    0 讨论(0)
  • 2020-12-16 19:09

    Edit your source code, so that you remove one line at a time, until the error disappears, and that should point you closer to the problem.

    0 讨论(0)
  • 2020-12-16 19:17

    what about this:

    try:
      if x:
          print 'before statement 1'
          statement1
          print 'before statement 2' #ecc. ecc.
          statement2
          statement3
      elif y:
          statement4
          statement5
          statement6
      else:
          raise
    
    except:
          statement7
    

    this is the straightforward workaround but I suggest to use a debugger

    or even better, use the sys module :D

    try:
          if x:
              print 'before statement 1'
              statement1
              print 'before statement 2' #ecc. ecc.
              statement2
              statement3
          elif y:
              statement4
              statement5
              statement6
          else:
              raise
    except:
        print sys.exc_traceback.tb_lineno 
        #this is the line number, but there are also other infos
    
    0 讨论(0)
提交回复
热议问题