What is an exception? Errors detected during program operation are called exceptions.
We can catch and handle these exceptions through the program, which involves the try and raise statements
First look at the Try statement:
Execution mode:
- First, execute a try clause
- If there is no exception, the EXCEPT clause is ignored after the try
- If an exception occurs when the TRY clause executes, then the remainder of the clause is ignored, and if the exception matches the specified exception after the except, the EXCEPT clause is executed and then executed down
- If there is no except match, it will be uploaded to the previous try, if the processing statement can not be found, will become an unhandled exception, terminate the program run, display the prompt message
- When the try clause does not throw an exception, and you need to execute some code, use the Else statement
- Finally is executed in any case, even if the try clause exits the page through the Break,continue,return statement.
Attention:
- At most only one except branch is executed
Second raise statement (throws an exception):
The raise statement is used to force the specified exception to occur, the first parameter specifies the name of the throw exception, and the second parameter that specifies the exception
Finally look at the exception parameter problem:
When an exception occurs, there may be a subordinate value, a parameter that exists as an exception, whether the parameter exists, what type it is, and what type of exception it depends on.
After the exception name, you can also specify a variable, which is bound to an exception instance, stored in the Instance.args parameter
Operation Result:
Python Getting Started Guide 4, Errors and exceptions