1

How can I get where exactly in my code there was an error? I need to know if it was inside or outside a function and the absolute line number inside the script (or at least inside the function).

Looking at the very schematic example below this lines:

Let's say there's an error inside function func_b. I want to print: Error in func_b, line 4

Right now I'm using

sys.exc_info()[2].tb_lineno 

to get the line number, but it prints: Error in my_script.py, line 8 which is not what I want.

If there's an error before executing any function I want to print: Error in my_script.py, line 6

def func_a():   
  #do something    
def func_b():   
  #do something
try:   
  #do something   
  func_a()   
  func_b()    
except Exception as e:   
  print(...) # print where error occurred.
DMS02
  • 149
  • 4
  • I think this will help you: https://stackoverflow.com/questions/6810999/how-to-determine-file-function-and-line-number of simple `print __file__`, and `print __line__` should work – darvark Oct 26 '17 at 11:08

0 Answers0