[Python]python, handling exception information using Traceback

Source: Internet
Author: User

Tag: Pat time value Dice ack localtime ALT requires for

Recently wrote a program that can get a list of folders and files updated by the specified folder within a set time, and do something based on the list of updates obtained. Because the program is put on the server to run, in order to ensure that the program in the process of running, from time to time to jump out of some unusual information to scare other users, in the program added exception handling. Organize the information on the web and try to use Sys.exce_info () and Traceback together. The effect is good, the following information is the way I am currently handling exceptions, where type is the type of exception, value is the cause of the exception, Traceback is the exception information through the Traceback Chase, to locate the code that caused the exception.

2016-11-07 22:07:56-------------------------------'exceptions. TypeError'> not strtraceback: [(' filename ' ) ' function name  ' , ' the line of code where the exception occurred ')]

In Try...except, the following two lines are used to record the exception condition. Depending on how the program continues after an exception, you need to look at the specific requirements.

TP,VAL,TD = sys.exc_info () log.logerexception (TP,VAL,TD)

The specific code is as follows

1 ImportOS2 Import Time3 ImportTraceback4 ImportSYS5 6 deflogerexception (TP,VAL,TD):7EType =STR (TP)8Evalue =Str (val)9ETB =TRACEBACK.EXTRACT_TB (TD)TenErrorMsg ="Type:"+ EType +"\ n" OneErrorMsg + ="Value:"+ Evalue +"\ n" AErrorMsg + ="Traceback:"+ STR (ETB) +"\ n" - WriteToFile (errormsg) -      the defWriteToFile (errormsg): -LogFilePath = Os.path.abspath ('.') +"/log" -     if  notos.path.exists (LogFilePath): - Os.mkdir (LogFilePath) +  -LogFile = Time.strftime ("%y%m%d", Time.localtime ()) +". txt" +fp = open (LogFilePath +"/"+ LogFile,"a") Aisotimeformat="%y-%m-%d%x" atHappeningtime =time.strftime (Isotimeformat, Time.localtime ()) -Usermsg ="" -Usermsg + = Happeningtime +"\ n-------------------------------\ n" -Usermsg + =errormsg -Fp.write (usermsg +"\ n") -Fp.close ()
View Code

[Python]python, handling exception information using Traceback

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.