Python learning-errors, debugging and testing

As a guide page

During the running of the program, various errors will always be encountered.

Some errors are caused by programming problems, such as outputting a string as an integer result. This kind of error is usually called a bug , and a bug must be fixed.

Some errors are caused by user input, such as asking the user to enter an email address, and the result is an empty string. This kind of error can be handled by checking the user's input.

There is another type of error that is completely unpredictable during the running of the program. For example, when writing a file, the disk is full and cannot be written to, or the network is suddenly disconnected when data is fetched from the network. This kind of error is also called an exception , and usually must be handled in the program, otherwise, the program will terminate and exit due to various problems.

Python has a built-in exception handling mechanism to help us with error handling.

In addition, we also need to track the execution of the program to see if the value of the variable is correct. This process is called debugging . Python's pdb allows us to execute code in a single step.

Finally, writing tests is also very important. With a good test, you can run the program repeatedly after modification to ensure that the program output conforms to the test we wrote.

Guess you like

Origin blog.csdn.net/qq_44787943/article/details/112562643