Correct Line number when doing exception handling in python -
i use structure capturing , recording exception handling
1. import sys 2. 3. def break_python(): 4. test1 = 1/0 5. 6. def main(): 7. #all main program functionality included in section 8. break_python() 9. 10. if __name__ == "__main__": 11. try: 12. main() 13. except exception e: 14. print dir(e) 15. print e.message 16. type1,details,tb = sys.exc_info_() 17. print type1 18. print details 19. print tb 20. line_no = tb.tb_lineno 21. print "line # " + str(line_no) 22. print "failed run program"
when run primary program structure works line number print line 12 corresponding main() function call when want point function , line number of individual error, in case line 4 under function break_python()
i think looking traceback.print_exc(file=sys.stdout)
see https://docs.python.org/2/library/traceback.html#traceback-examples
Comments
Post a Comment