I have a code similar to this:
try:
if x:
statement1
statement2
statement3
elif y:
statement4
statement5
statement6
else:
raise
except:
statement7
Here, I am sure that the exception occurs in If x:
block, but I would like to know in which statement of If x:
block the exception occurs. Is there a way to get the line number where the exception occurs?
Regards,
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.
what about this:
this is the straightforward workaround but I suggest to use a debugger
or even better, use the sys module :D
If you restructure the code like so, you should get a line number when the exception is raised again:
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
Yields
Using a general except statement is usually a bad programming practice, so you should specify in your except statement what exception you want to catch. ( like except ValueError: )
Moreover, you should surround with a try except structure the bits of code that are supposed to be raising an exception.
You should wrap the statements you care about more tightly. Extracting the line number from the traceback is going to be involved and fragile.