Exception Handling
1. The exception handling action is:
- I do not want to terminate the program
- If something goes wrong, it requires special handling to ensure the stability and robustness of the program
2. Capture exception can use try / except statement
- try / except statement is used to detect errors in the try block, so that the abnormality information except statement capture and processing.
- If you do not want to end your program when an exception occurs, simply try to capture it in.
3. Syntax 1
- try ... except ... else ...
- When no exception occurs, else statements will be executed
- When there is an abnormality in the try block error, except in the code block is executed
- You can not pass the processing result placeholders
# Catch all exceptions a the try : ... except: ... else: ... # Catch all exceptions two the try : ... except Exception,e: print e else: ... # Simultaneously capture multiple exceptions, simultaneous processing, the same reaction to make the different exception the try : Listing 1 the except (error type 1, type 2 error, the error type 3, ...): Code segment 2 else: ... # Simultaneously capture multiple exceptions, simultaneous processing, react differently to different exception the try : Listing 1 the except (Error Type 1): Code segment 2 the except (error type 2): Code segment 3 except(...): ... else: ...
Example:
# Specifies the exception type the try : FH = Open ( " d: \\ testfile " , " r " ) Print fh.read () the except IOError: Print " Error: file not found or read the file failed ," the else : Print " content written to the file successfully ." fh.close() # Get abnormality by Exception the try : . 1/ 0 the except Exception, E: Print E the else : Print " OK " # Used except without any exception type with the try : . 1/ 0 except : Print " error " the else : Print " OK "
4. Syntax 2
- try ... except ... finally ...
- Whether or not an exception will be executed finally captured
try: Listing 1 the except (error type 1, type 2 error, the error type 3, ......): Code segment 2 finally: Code segment 3
The initiative thrown
grammar:
- raise the types of errors - to raise the program execution when an exception is thrown directly
- Note: Error type must be a class, and this class is a subclass of Exception
# Input the age, if the input age range 0-100 not, the program would crash Age = int (INPUT ( ' Age: ' )) IF Age> 100 or Age < 0: The raise a ValueError
6.python standard abnormality as follows
BaseException | Base class for all exceptions |
SystemExit | Interpreter exit request |
KeyboardInterrupt | User Interrupt Executing (usually enter ^ C) |
Exception | General Error base class |
StopIteration | Iterator no more value |
GeneratorExit | To notify the exit exception generator (generator) occurs |
StandardError | All standard exceptions built base class |
ArithmeticError | All numerical errors base class |
FloatingPointError | Floating-point calculation error |
OverflowError | Exceeds the maximum limit value calculation |
ZeroDivisionError | In addition to (or modulus) of zero (all data types) |
AssertionError | Assertion failure |
AttributeError | Object does not have this property |
EOFError | No built-in input, to reach the EOF marker |
EnvironmentError | OS Error base class |
IOError | Input / output operations to fail |
OSError | Operating system error |
WindowsError | System call fails |
ImportError | Import module / object failed |
LookupError | Invalid class data base query |
IndexError | Without this sequence index (index) |
KeyError | Without this key mapping |
MemoryError | Memory overflow error (for Python interpreter is not fatal) |
NameError | Undeclared / initialize objects (no attributes) |
UnboundLocalError | Local access uninitialized variables |
ReferenceError | Weak reference object (Weak reference) have been trying to access a garbage collection of |
RuntimeError | General runtime error |
NotImplementedError | The method has not been implemented |
SyntaxError | Python syntax error |
IndentationError | Indentation errors |
TabError | Tab and space mix |
SystemError | General interpreter system error |
TypeError | Invalid type of operation |
ValueError | Invalid parameter passed |
UnicodeError | Unicode related errors |
UnicodeDecodeError | Error when decoding of Unicode |
UnicodeEncodeError | Unicode encoding error |
UnicodeTranslateError | Unicode conversion error |
Warning | Warning base class |
DeprecationWarning | Warning about deprecated features |
FutureWarning | Warning about the future structure of the semantics have changed |
overflow Warning | The old warning about automatically promoted to a long integer (long) of |
PendingDeprecationWarning | Will be warned about the characteristics of the waste |
RuntimeWarning | Suspicious behavior (runtime behavior) run-time warning |
SyntaxWarning | Suspicious Warning grammar |
UserWarning | User code generated warning |