python abnormalities (concept, capture, transfer, throws)

01. unusual concept

Program at run time, if the Python interpreter encounters an error, stop program execution, and prompts some wrong information , which is abnormal

The program stops executing and an error message this action, we usually call: throw (raise) abnormal

When developing the program, it is difficult for all of the special circumstances everything is handled by abnormal capture can be done centralized process for emergencies, so as to ensure program stability and robustness

02. catch the exception

2.1 simple catch exceptions grammar

Program development, if the execution of some code can not be determined correctly , can increase the try (try) to catch the exception

Catch exceptions simplest syntax:


13717038-bfd326274341ed29.png

try try , try to write the code below, uncertain whether the code can be executed normally

except if not , write code below attempt fails

Simple exception catching drills - requires the user to enter an integer


13717038-0740f310af9d4c29.png

2.2 type of error trapping

During program execution, you may encounter different types of exceptions , and the need for different types of exceptions, respond differently , this time, we need to capture the type of error

The syntax is as follows:


13717038-7b6b9e7e455277f6.png

When the Python interpreter throws an exception , the first word in the last line of the error message, type is wrong

Capture exception type exercise - require user input integer

demand

It prompts the user to enter an integer

Integer by using the user input and output 8


13717038-68270395c7b035c4.png

Capture unknown error

In the development, to anticipate all possible errors , or have some difficulty

If you want the program to any error occurs regardless , will not because Python interpreter throws an exception has been terminated , you can add another except

The syntax is as follows:


13717038-f505005b7449122e.png

2.3 abnormal capture the full syntax

In the actual development, in order to be able to deal with complex anomalies, abnormal complete syntax is as follows:

prompt:

Related scenarios complete syntax, in the follow-up study, combined with actual cases will be better understood

Now there is a first impression to this grammatical structure


13717038-d13fb4c7e505d29b.png

Code else only when there is no abnormality will be executed

finally, whether or not there is an exception code that will be executed

Prior to the exercise of a complete capture abnormal code is as follows:


13717038-a8db029e4bbffa0b.png


03. abnormal transfer

Abnormal transfer - when the function / method execution exception occurs , it will pass the exception to the function / method call party

If the transfer to the main program , still no exception handler , the program will be terminated

prompt

In development, may increase in the main function abnormalities capture

The other function calls in the main function, as long as the abnormal, will be passed to the main function abnormalities captured in

This eliminates the need in the code to increase the number of exception traps , to ensure clean code

demand

The demo1 defined function () prompts the user and returns an integer

Defined functions demo2 () call demo1 ()

Demo2 call in the main ()


13717038-e3fa5aadecb60e0e.png


04. throws raise an exception

4.1 application scenarios

In development, in addition to code execution error Python interpreter throws than an exception

But also according to the application -specific business needs active thrown

Examples

The user is prompted to enter a password , if the length is less than 8 , throws an exception

note

The current function is only responsible for prompting the user for a password if the password length is incorrect, other functions require additional processing

So you can throw an exception , the other functions need to be addressed catch the exception

4.2 throws an exception

Exception provides a Python exception class

In the development, if they meet a specific business need , you want to throw an exception , you can:

Creating an Exception of objects

Using the raise keyword thrown exception object

demand

Input_password defined function, the user is prompted to enter a password

If the user inputs the length <8, an exception is thrown

If the user inputs the length> = 8, returns the input password


13717038-97500c51578907bb.png

Guess you like

Origin blog.csdn.net/weixin_33991727/article/details/91001227