python运行时错误,可以转储文件吗?
我正在使用 libcurl 深度学习网页,然后扫描其中的数据并使用其中一个链接执行某些操作。 然而,偶尔页面会与我不同,除非因此我提取了错误的数据并且 pycurl 抛出异常。 我尝试找到 pycurl 的异常名称,但没有成功。
有没有一种方法可以让我获得执行函数的回溯,以便我可以转储文件,以便我可以查看文件输入并查看我的代码是否出错了?
I am using libcurl to DL a webpage, then i am scanning it for data and doing something with one of the links. However, once in a while the page is different then i except thus i extract bad data and pycurl throws an exception. I tried finding the exception name for pycurl but had no luck.
Is there a way i can get the traceback to execute a function so i can dump the file so i can look at the file input and see were my code went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sys.excepthook 可能会在这里帮助你,你可以在其中设置全局异常处理程序。 我不确定 pycurl 异常是如何处理的,它是一个绑定库,但它可能可以将其重新分配给通用函数。 类似于:
这个异常挂钩函数很容易成为一个可以访问需要转储的文件的实例方法。
sys.excepthook may help you here, where you can set a global exception handler. I am not sure how pycurl exceptions are handled, it being a binding library, but it will probably work to reassign it to a generic function. Something like:
This exception hook function could easily be an instance method that has access to the file that needs dumping.
您可以使用通用异常处理程序。
这将编写一个漂亮的日志,其中包含您正在查找的异常信息。
You can use a generic exception handler.
This will write a nice log that has the exception information you're looking for.
您能否捕获主块中某处的所有异常并使用 sys.exc_info() 获取回调信息并将其记录到您的文件中。 exc_info() 不仅返回异常类型,还调用回溯,因此应该有出错的信息。
Can you catch all exceptions somewhere in the main block and use sys.exc_info() for callback information and log that to your file. exc_info() returns not just exception type, but also call traceback so there should information what went wrong.