我正在使用Twister来构建服务器.我也在维护服务器错误日志.问题是,如果我让异常一直向上运行,它会崩溃当前连接并断开用户连接,所以很明显我附加了一个裸,除了抓住其他所有东西.
一旦我发现了什么,是否有办法将回溯作为一个字符串,以便我可以将它存储在某个地方/自己打印而不提高它并让Python在程序崩溃时为我打印它?
解决方法:
回溯模块包含一些辅助函数,用于打印和检查回溯(对于exameble,traceback.print_tb) – 但重要的是回溯信息本身存储在模块sys上的“interpreter global”变量 – sys.exc_traceback中.
引用自:
Before an except clause’s suite is executed, details about the
exception are assigned to three variables in the sys module:
sys.exc_type receives the object identifying the exception;
sys.exc_value receives the exception’s parameter; sys.exc_traceback
receives a traceback object…
您可以将sys.exc_traceback对象作为参数传递给traceback.print_tb,以便在except子句中将回溯打印到stdout.
标签:python,exception,traceback