# exception Handling
# exception
-Exceptions are problems that occur when the grammar logic is working correctly
-In Python, an exception is a class that can handle and use
Baseexception base class for all exceptions
Systemexit interpreter Request Exit
keyboardinterrupt user interrupt execution (usually input ^c)
exception base class for general errors
Stopiteration iterator with no more values
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 has no built-in input to reach the EOF tag
EnvironmentError base class for operating system errors
ioerror input/output operation failed
OSError Operating system error
windowserror system call failed
importerror Import Module/object failed
Lookuperror base class for invalid data queries
This index is not in the Indexerror sequence (index)
This key is not in the Keyerror map
memoryerror Memory overflow error (not fatal for Python interpreter)
Nameerror Object not declared/initialized (no attributes)
unboundlocalerror access to uninitialized local variables
referenceerror Weak reference (Weak reference) attempts to access objects that have been garbage collected
RuntimeError General run-time errors
Notimplementederror methods that have not yet been implemented
Syntaxerrorpython Syntax error
indentationerror Indent Error
Taberrortab and spaces mixed
SYSTEMERROR General interpreter system error
TypeError operations that are not valid for types
valueerror Invalid arguments passed in
Unicodeerrorunicode Related Errors
Unicodedecodeerrorunicode decoding Error
Unicodeencodeerrorunicode Encoding Error
Unicodetranslateerrorunicode Conversion Error
base class for warning warnings
deprecationwarning warning 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 runtime behavior (runtime behavior)
syntaxwarning warning of suspicious syntax
userwarning warning for user code generation
The above content is quoted from "Https://www.cnblogs.com/zln1021/p/6106185.html", here to express our thanks
#异常处理
-There is no guarantee that the program will run correctly
-However, it is important to ensure that the problems of the program are properly handled in the worst case
The full syntax of the exception handling module for-python is:
Try:
try to implement an action
If the exception is not present, the task can be completed
If an exception occurs, throw the exception out of the code block and try to resolve the exception
except exception type 1:
Solution 1: Try to handle exceptions here
except exception type 2:
Solution 2: Try to handle exceptions here
except exception type 3:
Solution: Use the same processing for multiple exceptions
except:
Solution: Solutions for all exceptions
Else:
If there are no exceptions, this code will be executed
finally:
code to execute, whether or not an exception is available
-Process Flow
1. Execute the statement below the try
2, if an exception occurs, in the except statement to find the corresponding exception to handle
3. If there is no exception, the Else statement is executed
4, finally, whether there is no exception, will execute the finally statement
-Note: except for except (at least one), the others are optional
#Simple Exception case-1#give a hintTry: num int (input ("Please input your number:")) M= 100/NumPrint("The result of the calculation is {0}". Format (m))#after catching an exception, instantiate the exception, and the error message will be in the instance#Note the following notation#The following statement captures the ZERODIVISIONERROR exception and instantiates the EexceptZerodivisionerror as E:Print("you entered the wrong value") Print(e)#Exit is exiting the programexit ()#Simple Exception Case-2#give a hintTry: num int (input ("Please input your number:")) M= 100/NumPrint("The result of the calculation is {0}". Format (m))#do not know what will happen, you can use exception, all the exceptions are inherited exception#Also, this statement can only be placed on the last one to exclude unknown errorsexceptException as E:Print("I don't know what's wrong, it's a mistake anyway.") Print(e)#Exit is exiting the programExit ()
-Note that when an exception is handled, it does not continue to look down, and the finally statement or other large statement is
#user throws an exception manually-when the user wants to throw an exception in some cases, you can use the-Raisekeyword throws an exception#Case-1Try: Print("I love Siqi") Print(2.34574) #manually throwing an exception #pay attention to grammar RaiseValueErrorPrint("wrong")exceptNameerror as E:Print("Error 1")exceptValueError as E:Print("Error 2")exceptException as E:Print("Error 3")finally: Print("statements that must be executed") #Case-2#define your own exceptions#Note: Custom exceptions must be subclasses of system exceptionsclasshouzierror (valueerror) PSSTry: Print("I love Siqi") Print(2.34574) #manually throwing an exception #pay attention to grammar RaiseValueErrorPrint("wrong")exceptNameerror as E:Print("Error 1")exceptHouzierror as E:Print("Monkey's wrong .")exceptException as E:Print("Error 3")finally: Print("statements that must be executed") #Else Statement CaseTry: num int (input ("Please input your number:")) M= 100/NumPrint("The result of the calculation is {0}". Format (m))exceptException as E:Print(str (e))Else:Print("No Exception")finally: Print("statements that must be executed")
# About Custom Exceptions
-A custom exception is recommended as long as it is a raise exception
-At the time of the custom exception. Generally includes the following:
-Custom exception code for exception occurrence
-Customize the problem prompt after an exception occurs
-Customize the number of rows in which the exception occurred
-The ultimate goal is to quickly find the wrong location once an exception occurs
python-Exception Handling-20