Python exception module Traceback usage examples
Traceback.print_exc () #把返回信息输出到控制台
fp = Stringio.stringio () #创建内存文件对象
Traceback.print_exc (FILE=FP) #把返回信息写到文件中去
message = Fp.getvalue () #获得信息
Figuring out Python Traceback
In fact, the TRACEBACK.PRINT_EXC () function is simply a shorthand form of the traceback.print_exception () function, and the data they obtain from the exception are obtained by the Sys.exc_info () function.
Traceback module uses Traceback object
Print_exception (etype, Value, tb[, limit[, file])
Print_exc ([limit[, file]]) is a shorthand for print_exception print_exception (Sys.exc_type, Sys.exc_value, Sys.exc_traceback, limit , file) (it uses Sys.exc_info ())
FORMAT_EXC ([limit]) (much like print_exc (limit)) directly returns the string, does not output to the stream, does not have to use the above Stringio to get the string, in 2.4 of the newly added
Limit parameters, which limit the number of error layers returned, that is, when function B is called, an exception occurs in function B, and if limit=1, only the error in display function A is returned, and if limit is set, all returns.
The file parameter, specifying the output to the document.
Other methods See official documentation Traceback-print or retrieve a stack traceback