Exception handling, which is essential in programming. Mistakes can happen, and users cannot do exactly as the developer wishes, and there are some unpredictable errors, such as Web requests.
Once the program encounters an exception, it is terminated, and the error stack is thrown from the bottom, and cannot be executed smoothly as planned. Therefore, we need to catch exceptions, and sometimes even actively throw exceptions.
1.try...except Statements
This is the simplest exception capture. The program tries to execute the code in the try statement , and when an exception occurs, it jumps to the except statement to execute.
The exception type is not specified after except, and any exception is caught, and the except in the sample code is executed regardless of the error.
1 Try : 2 Print ('dosomething ... ' )3except:4 print(' found exception ' ')
baseexception is a generic exception parent class general exception (Special exception: For example, execution exit () exits the program, which Python considers to be a Systemexit exception).
1 Try : 2 Print ('dosomething ... ' )3except baseexception:4 print(' General exception found ')
Some exceptions in Python are dedicated exception classes are used for capture and can be captured for such errors using Python's built-in error types for precise processing;
The as variable can assign the information attached to the exception to the variable and print it out in the except statement, which is a very friendly approach;
of course, do not write as variables is also feasible.
1 Try : 2 Print (1/ 0) 3 except Zerodivisionerror as E: 4 Print (e)
A try...except statement, allowing only one try statement, can exist multiple except statements;
If more than one exception is caught, it is captured from the top down until the type of the exception is caught.
Try: Print('Do something ...')exceptTypeError:Print('Data type Error')exceptValueError:Print('Value Error')exceptKeyerror:Print('Key Error')exceptAttributeerror:Print('property error for Object')
Attention
Zerodivisionerror, but exception is the parent class of Zerodivisionerror. So the program captures the exception and stops, the output ' discovery error ' instead of the output ' 1 ' in our plan cannot be divided by 0 '
1 Try : 2 Print (1/ 0) 3 except Exception: 4 Print (' error found ') 5 except Zerodivisionerror: 6 Print ('1 cannot be divided by 0')
Writing multiple exception classes in tuples is the same as multiple except, except that the code is less. The disadvantage is that they cannot be handled separately, the only difference is:
If followed by the as variable , the information for the type of exception that is captured is assigned to the variable
1 Try : 2 Print (1/ 0) 3 Print (1 + None) 4 except (Zerodivisionerror, TypeError) as E: 5 Print (e)
Else statement
Finally statement
1 Try:2 Print('Do something ...')3 except:4 Print('found exception')5 Else:6 Print('The Try statement is executed normally and no code such as Return or exit () is executed')7 finally:8 Print('whether or not an exception occurs, it will execute')
2. Custom Exceptions
define a class that inherits from exception or baseexception
For additional error prompts, you can define the __str__ method and return an error prompt
Usually used to do special processing for the program
1 class Myerror (Exception): 2 def __str__ (self): 3 return ' A custom exception occurred '
3.raise Statements
Generate errors proactively and can be captured
1 class Myerror (Exception): 2 def __str__ (self): 3 return ' A custom exception occurred ' 4 5 6 Raise Myerror # throws out a defined exception and appends ' a custom exception occurred '
technology is limited, there may be missing knowledge points, please understand!
Python Exception handling