try_except_finally

Blog:  Python in the try / the except / the else / a finally   Python documentation: errors and exceptions

As with other languages, in python, try / except statement is mainly used to treat a number of procedural anomalies arising in the course, such as syntax errors normal execution (python scripting language is not compiled as part of the syntax is detected in the implementation process , issued after an error exception message), data divide by zero error, such as the value of the variable has never been defined; and try / finally statements are mainly used in the case regardless of whether an exception occurs, you need to perform some cleanup work, as in communication process, regardless of whether the communication error occurs, will need to complete in a communication network connection on or off when an error occurs. Despite the different try / except and try / finally role, but in practice, generally be programmed together in combination they try / except / else finally form / to achieve stability and better design flexibility.

By default, the execution of the program segment, if no processing try / except the abnormal message execution of a script file generated automatically sent to the program calls the terminal, such as python shell, the python shell abnormality message the default processing is terminated execution of the program and print specific error message. This is also the executive errors in the python shell which appears in print origin of the error message.

Guess you like

Origin www.cnblogs.com/dulun/p/11573212.html