我目前使用此结构来捕获和记录我的异常处理
1. import sys
2.
3. def break_python():
4. test1 = 1/0
5.
6. def main():
7. #All main program functionality is included in this section
8. break_python()
9.
10. if __name__ == "__main__":
11. try:
12. main()
13. except Exception as 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 to run program"
当我运行它作为我的主程序结构时,一切正常但是当我真的希望它指向单个错误的函数和行号时,行号将打印为对应于main()函数调用的第12行。这个案例第4行在函数break_python()
下答案 0 :(得分:2)
我认为您正在寻找traceback.print_exc(file=sys.stdout)
见https://docs.python.org/2/library/traceback.html#traceback-examples