Python fault tolerance,
# Try: release T: else:
# Why is it fault tolerance? Let's talk about the error first. The error mentioned here is not a bug left in the script because it is so careless or because it cannot be removed. The so-called "let down" means skipping this error, to detect and correct the errors during the test, the fault tolerance error cannot be determined before the script is executed. For example, when a crawler is writing, it crawls to many pages, I don't know whether these pages can be opened or not. It may time out. At this time, the process cannot be stopped due to this error. If there are tens of thousands of addresses, it may take several years to climb. So here we use fault tolerance, skip this error, and write the error into a log. After all the logs are completed, we can check the log to see which pages report errors, then process the batch of pages separately.
# Take opening a file as an Example
# Create 10 files for I in range (10): file_name = 'a_0000d.txt '% I new_file = open (file_name, 'w') new_file.close () # Then read 11 files, note: There are 11 files, that is to say, there must be one file that does not exist, and an error will be reported. Now I want to skip this error and write it into the log # Write the log function and write it one at a time, the name is write_logfor I in range (11): file_name = 'a_0000d.txt '% I # open_file = open (file_name, 'R'). When 11th files are read, an error will be reported when it is 10, so try: open_file = open (file_name, 'R') # There is a possible error code after try t: write_log ('Open file fail! ') # This is the handling of errors. If an error occurs, the open file will be fail! Write logs. This is acceptable, but it is not recommended that you output this error and write it together in the log. The following is the case # The normal syntax is for I in range (11 ): file_name = 'a_0000d.txt '% I try: open_file = open (file_name, 'R') handle T IOError, e: # This IOError is an IO error. If this error occurs, write the error content to the e variable, or change IOError to Exception, so that no matter what error occurs, it will be written to the e variable, but to clarify the error, it is better to specify the Error Type write_log ('% s open file fail! % S') % (file_name, e) # Write the error file name and error message to the log else: open_file.close () # although it is fault tolerant, but the opened files still need to be closed, but they are not opened, that is, 10. They cannot be closed. Therefore, to be added to else, the else function is executed normally, run the command # open_file.close () next to else to write it out. If you want to close it whether it is correct or not, an error will be reported when you open it 10, that is, it is not opened at all. Why should we close it?
I forgot to add a bunch of errors and copied them. There are many Baidu websites. For details, refer
Exception name |
Description |
BaseException |
All abnormal base classes |
SystemExit |
Interpreter request to exit |
KeyboardInterrupt |
User interrupted execution (usually input ^ C) |
Exception |
Base class with regular errors |
StopIteration |
The iterator does not have more values. |
GeneratorExit |
Generator exception to notify exit |
StandardError |
All base classes with built-in standard exceptions |
ArithmeticError |
Base classes with incorrect numeric calculation |
FloatingPointError |
Floating Point Calculation Error |
OverflowError |
The value operation exceeds the maximum limit. |
ZeroDivisionError |
Except (or modulo) zero (all data types) |
AssertionError |
Assertion statement failed |
AttributeError |
The object does not have this property. |
EOFError |
No built-in input, reaching the EOF tag |
EnvironmentError |
Operating System Error base class |
IOError |
Input/Output operation failed |
OSError |
Operating System Error |
WindowsError |
System Call failed |
ImportError |
Import module/object failed |
LookupError |
Base class for invalid data query |
IndexError |
This index is not found in the sequence) |
KeyError |
The ing does not contain this key. |
MemoryError |
Memory overflow error (not fatal for Python Interpreter) |
NameError |
Object not declared/initialized (no attribute) |
UnboundLocalError |
Access uninitialized local variables |
ReferenceError |
Weak reference attempts to access garbage collection objects |
RuntimeError |
General running errors |
NotImplementedError |
Unimplemented Methods |
SyntaxError |
Python syntax error |
IndentationError |
Indentation Error |
TabError |
Mix Tab and Space |
SystemError |
General interpreter system error |
TypeError |
Operation that is invalid for the Type |
ValueError |
Invalid parameter passed in |
UnicodeError |
Unicode errors |
UnicodeDecodeError |
Unicode decoding error |
UnicodeEncodeError |
Unicode Encoding Error |
UnicodeTranslateError |
Unicode Conversion error |
Warning |
Warning base class |
DeprecationWarning |
Warning about discarded features |
FutureWarning |
Warning about future semantic changes in Construction |
OverflowWarning |
Old warning about automatic upgrade to long (long) |
PendingDeprecationWarning |
Warning that features will be discarded |
RuntimeWarning |
Warning of suspicious running behavior (runtime behavior) |
SyntaxWarning |
Warning of suspicious syntax |
UserWarning |
Warning generated by user code |