简体   繁体   English

Python Stacktrace模块回溯行错误

[英]Python stacktrace module traceback line error

I have the following Python program: 我有以下Python程序:

import traceback
import sys

try:

    3/0
except OverflowError as e:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    formatted_lines = traceback.format_exc().splitlines()

    print(" It looks like in the arithmetic operation :" , formatted_lines[2], " )  #gets the offending line
    print (at line number " , exc_traceback.tb_lineno )  #gets the line number

except ZeroDivisionError as e:
   exc_type, exc_value, exc_traceback = sys.exc_info()
   formatted_lines = traceback.format_exc().splitlines()
   print(" It looks like in the arithmetic operation :" , formatted_lines[2], " )  #gets the offending line
   print (at line number " , exc_traceback.tb_lineno )  #gets t

For simple programs as above the stacktrace returns the correct line number, but for more complicated methods like below Python throws more stacktraces (the latest call being the last), is there a way to figure out the index of the stacktrace ex: formatted_lines[2] to get the latest call. 对于像上面这样的简单程序,stacktrace返回正确的行号,但是对于像下面这样的更复杂的方法,Python会抛出更多的stacktrace(最后一次调用是最后一个),有没有办法找出stacktrace的索引,例如: formatted_lines[2]获取最新通话。

try:
 def prize():
    print("hello")

 def main():
    prize()

Catch:
.....

any help would be appreciated. 任何帮助,将不胜感激。


Also tried this: 还尝试了这个:

import traceback
import sys
import linecache


try:

      2/0

except ZeroDivisionError as e:
        filename = exc_traceback.tb_frame.f_code.co_filename
        lineno = exc_traceback.tb_lineno
        line = linecache.getline(filename, lineno)
        print "exception occurred at %s:%d: %s" % (filename, lineno, line)

I get an error on the last line "invalid syntax" 我在最后一行“无效语法”中收到错误

When I just try: 当我尝试时:

print  (filename, lineno, line)

I get an error: 我收到一个错误:

 Traceback (most recent call last):
  File "C:\Users\Anu\Desktop\test.py", line 39, in <module>
    filename = exc_traceback.tb_frame.f_code.co_filename
NameError: name 'exc_traceback' is not defined

Please don't try to parse stack traces using the output of format_exc . 请不要尝试使用format_exc的输出来解析堆栈跟踪。 That's only meant to produce a human-readable stack trace. 这仅是为了产生人类可读的堆栈跟踪。

You should instead use linecache to get the offending line: 您应该改用linecache来获取有问题的行:

exc_type, exc_value, exc_traceback = sys.exc_info()
filename = exc_traceback.tb_frame.f_code.co_filename
lineno = exc_traceback.tb_lineno
line = linecache.getline(filename, lineno)
print("exception occurred at %s:%d: %s" % (filename, lineno, line))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:堆栈跟踪与回溯 - Python: Stacktrace vs Traceback 如何使用 Python 修复 Pyx 模块中的回溯错误? - How to fix traceback error in Pyx module with Python? 如何修复 Python 中的 Traceback 模块错误? - How to fix Traceback module error in Python? Python模块scrapy总是会产生Traceback错误 - Python module scrapy always gives Traceback Error 在 Python 错误上显示堆栈跟踪或行号 - Showing stacktrace or line number on Python error 适用于初学者的Python追溯模块 - Python traceback module for beginners 带模块名称的Python回溯 - Python traceback with module names 无法在python 3Traceback(最近一次调用为最新)中找出此错误:“文件” <stdin> ”,第1行,在 <module> NameError:未定义名称“ xlrd” - Cannot figure out this error in python 3Traceback (most recent call last): File “<stdin>”, line 1, in <module> NameError: name 'xlrd' is not defined 未知的 Python Selenium 错误:Traceback(最近一次调用最后一次):文件“D:\pythonProject\webscraping.py”,第 17 行,在<module> - Unknown Python Selenium error:Traceback (most recent call last): File "D:\pythonProject\webscraping.py", line 17, in <module> Python 回溯,即使没有显示完整的回溯,也显示发生错误的行 - Python traceback, show line where error occurred even if not showing complete traceback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM