重庆php网站建设,网站ip地址 转向域名,邯郸市网站建设,清远网站制作我正在尝试捕获所有异常错误#xff0c;然后在脚本结尾处使其引发/显示所有回溯...我有一个主脚本#xff0c;例如调用我的下标#xff1a;errors open(MISC/ERROR(S).txt, a)try:execfile(SUBSCRIPTS/Test1.py, {})except Exception:## Spread over two calls…我正在尝试捕获所有异常错误然后在脚本结尾处使其引发/显示所有回溯...我有一个主脚本例如调用我的下标errors open(MISC/ERROR(S).txt, a)try:execfile(SUBSCRIPTS/Test1.py, {})except Exception:## Spread over two calls of errors.write for readability in code...errors.write(strftime(%d/%m/%Y %H:%M:%S) )errors.write(traceback.format_exc() )try:execfile(SUBSCRIPTS/Test2.py, {})except Exception:## Spread over two calls of errors.write for readability in code...errors.write(strftime(%d/%m/%Y %H:%M:%S) )errors.write(traceback.format_exc() )errors.close()此脚本使用追溯模块从脚本中检索错误...在下一个示例中这是我当前脚本的样子for x in y:ifexample in x:for tweet in tweetlist:# Trytry:twitter.update_status(statustweet)# Do some other stuff here if it suceeeds like...printoo example worked# Damn it failed, grab the whole traceback?except Exception as reason:FailedTweet True# Do some other stuff here like...printI did other stuffif FailedTweet:print reason # Printing the reason because I dont know how to throw the exception error (full error)基本上存在一个大循环可能会在此行中出现错误twitter.update_status(statustweet)如果确实如此我希望它能够捕获回溯错误因为它在一个循环中然后在脚本完成时它可能不止一个 我希望它将所有回溯错误发送回主脚本以便将其全部写入错误文件。从代码的第一位向文件写入错误的示例# 17/08/2014 12:30:00# Traceback (most recent call last):# FileC:\Main.py, line 117, in execute_subscripts# execfile(SUBSCRIPTS/Test1.py, {})# FileSUBSCRIPTS/Test1.py, line 440, in # twitter.update_status(statusstring)# FileC:\Python27\lib\site-packages\twython\endpoints.py, line 90, in update_status# return self.post(statuses/update, paramsparams)# FileC:\Python27\lib\site-packages\twython\api.py, line 234, in post# return self.request(endpoint, POST, paramsparams, versionversion)# FileC:\Python27\lib\site-packages\twython\api.py, line 224, in request# content self._request(url, methodmethod, paramsparams, api_callurl)# FileC:\Python27\lib\site-packages\twython\api.py, line 194, in _request# retry_afterresponse.headers.get(retry-after))# TwythonError: Twitter API returned a 403 (Forbidden), This request looks like it might be automated. To protect our users from spam and other malicious activity, we cant complete this action right now. Please try again later.我将如何实现这一点这很难解释所以如果有什么不对的地方请询问。只需将回溯数据保存在列表中然后在循环完成后打印列表的内容。import tracebackreasons []for x in y:ifexample in x:for tweet in tweetlist:# Trytry:twitter.update_status(statustweet)# Do some other stuff here if it suceeeds like...printoo example worked# Damn it failed, grab the whole traceback?except Exception:reasons.append(traceback.format_exc())# Do some other stuff here like...printI did other stufffor reason in reasons:print reason# If you want to raise a single exception that shows the traceback for# each exception, you can do this:class ChainedException(Exception):def __init__(self, msg):msg The following exceptions occurred:{}.format(msg)if reasons:raise ChainedException(.join(reasons))ChainedException的示例用法reasons []for i in range(5):try:raise Exception(Blah {}.format(i))except Exception:reasons.append(traceback.format_exc())if reasons:raise ChainedException(.join(reasons))输出Traceback (most recent call last):Fileok.py, line 17, in raise ChainedException(.join(reasons))__main__.ChainedException: The following exceptions occurred:Traceback (most recent call last):Fileok.py, line 12, in raise Exception(Blah {}.format(i))Exception: Blah 0Traceback (most recent call last):Fileok.py, line 12, in raise Exception(Blah {}.format(i))Exception: Blah 1Traceback (most recent call last):Fileok.py, line 12, in raise Exception(Blah {}.format(i))Exception: Blah 2Traceback (most recent call last):Fileok.py, line 12, in raise Exception(Blah {}.format(i))Exception: Blah 3Traceback (most recent call last):Fileok.py, line 12, in raise Exception(Blah {}.format(i))Exception: Blah 4编辑如果您真的只在乎从整个例外列表中提出一个例外则可以这样做import tracebackreason Nonefor x in y:ifexample in x:for tweet in tweetlist:# Trytry:twitter.update_status(statustweet)# Do some other stuff here if it suceeeds like...printoo example worked# Damn it failed, grab the whole traceback?except Exception:reason sys.exc_info() # Were not putting it in a list because you only care about one.# Do some other stuff here like...printI did other stuffif reason:raise reason[0], reason[1], reason[2]请注意这仅适用于Python2.x。 如果您使用的是Python 3.x则需要使用以下代码if reason:raise reason[1].with_traceback(reason[2])嗯很好的逻辑。然后如何创建异常以便主脚本意识到存在错误/将错误记录到文件中您实际上想看到什么记录只是一个或多个异常发生的通知还是所有回溯您只能对父脚本提出一个例外那应该是什么发生的实际异常之一还是指示一个或多个状态更新失败的一般性异常Ryflex Ive更新了我的答案以显示我认为您正在寻找的东西。恩我只是再次浏览了twitter API即错误代码并且当代码通过for循环时我不会收到不同的错误代码因此在这种情况下只抛出一个完整的完整回溯traceback.format_exc()效果会更好如我的原始文章所示。 ;我只是不知道如何将回溯发送回主脚本...在将原因设置如下后我会做raise reason吗香港专业教育学院搜索系统文档但我找不到任何有关exc_info()的每个部分做什么...Ryflex如果没有在堆栈上的任何地方处理异常则返回包含三个None值的元组。否则返回的值为(类型值回溯)。它们的含义是type获取正在处理的异常的异常类型(一个类对象) value获取异常参数(其关联值或要引发的第二个参数如果异常类型是类对象则始终为类实例) traceback获取一个traceback对象(请参见参考手册)该对象在最初发生异常的位置封装了调用堆栈。