Python Fault tolerance

Source: Internet
Author: User

#try: Except:else:

#为什么叫容错呢, first of all to say that the mistake here is not because of sloppy or any reason in the script left the bug, this can not be allowed, the so-called tolerance is to skip this error, to be found and fixed in the test, the need to fault-tolerant error is not before the script execution can not determine whether the error is wrong, For example: In the writing crawler, will crawl to many pages, these pages I do not know can open, it is possible to timeout, this time can not be because of this error to stop the process to find the problem, then if there are tens of thousands of addresses, estimated to use a few years to climb. So here is the use of fault-tolerant, jump over this error, and write the error to a log, and so on, then to check the log, see which pages report errors, and then separate this batch of pages to process

#以打开文件为例

#Create 10 files first forIinchRange (10): file_name='A_%d.txt'%I new_file= Open (file_name,'W') New_file.close ()#then read 11 files, note, is 11 files, that is, there is a file must not exist, then will be an error, now I want to skip this mistake, and write to the log#Write log function One will write one, the name is called Write_log forIinchRange (11): file_name='A_%d.txt'%I#open_file = open (file_name, ' R ') when the 11th file is read, it will be error 10, so it cannot be  Try: Open_file= Open (file_name,'R')#the code behind the try is a possible error  except: Write_log ('Open File fail!')#this is wrong to deal with, that is, the open file fail! will be written to the log, this is possible, but not recommended, it is best to put this error output, and write together in the log, is the following#normal wording forIinchRange (11): file_name='A_%d.txt'%ITry: Open_file= Open (file_name,'R')  exceptIOError, E:#This ioerror is an IO error, that is, if it is the wrong words, the error is written in the e variable, you can also change the ioerror to exception, so whatever error, will be written to the e variable, but in order to clarify the error, or to specify the wrong type is better Write_log ('%s Open File fail!%s')% (file_name,e)#write the wrong file name and error content to the log  Else: Open_file.close ()#Although it is fault-tolerant, but the open file is still to be closed, but not open, that is, 10, cannot be closed, so to add to the else, else the function is normal execution, and then execute the command after the Else  #Open_file.close () This is to write off the outside, think whether the right or not to close, then open 10 when the error, that is, no open, then how to close it

Forgot to add a bunch of error, this is copied, Baidu has a lot, you can refer to the next

Exception name Description
Baseexception base class for all exceptions
Systemexit Interpreter Request exited
Keyboardinterrupt User interrupt execution (usually input ^c)
Exception base class for general errors
Stopiteration There are no more values for iterators
Generatorexit Generator (generator) exception occurred to notify exit
StandardError Base class for all built-in standard exceptions
Arithmeticerror base class for all numeric calculation errors
Floatingpointerror Floating-point calculation error
Overflowerror Numeric operation exceeds maximum limit
Zerodivisionerror Except (or modulo) 0 (all data types)
Assertionerror Assertion statement failed
Attributeerror Object does not have this property
Eoferror No built-in input, EOF Mark reached
EnvironmentError Base class for operating system errors
IOError Input/output operation failed
OSError Operating system error
Windowserror System call failed
Importerror Failed to import module/object
Lookuperror base class for invalid data queries
Indexerror This index is not in the sequence (index)
Keyerror This key is not in the map
Memoryerror Memory overflow error (not fatal for Python interpreter)
Nameerror Object not declared/initialized (no attributes)
Unboundlocalerror To access uninitialized local variables
Referenceerror Weak references (Weak reference) attempt to access objects that have been garbage collected
RuntimeError General run-time errors
Notimplementederror Methods that have not been implemented
SyntaxError Python syntax error
Indentationerror Indentation Error
Taberror Tab and Space Mix
Systemerror General Interpreter system error
TypeError An operation that is not valid for type
ValueError Invalid parameter passed in
Unicodeerror Unicode-related errors
Unicodedecodeerror Error in Unicode decoding
Unicodeencodeerror Unicode encoding Error
Unicodetranslateerror Unicode Conversion Error
Warning Base class for warnings
Deprecationwarning Warnings about deprecated features
Futurewarning Warning about the change in the construction of future semantics
Overflowwarning Old warning about auto-promotion to Long integer
Pendingdeprecationwarning Warnings about attributes that will be discarded
Runtimewarning Warning for suspicious run-time behavior (runtime behavior)
Syntaxwarning Warning of suspicious syntax
Userwarning Warnings generated by user code

Python Fault tolerance

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.