In the execution of the program, it is unavoidable to encounter for some reason, such as the input and output caused by a fatal error (such as because the input file name is wrong, resulting in the inability to run the relevant code. )。 At this point you do not want the program to hang up directly, but by displaying some information to make it end smoothly. At this point, you can use the try,except and else three keywords to form a well-inclusive program.
Explain three key words separately:
Try: Execute a tentative statement that may be faulted, that is, the statement inside can cause a fatal error that prevents the program from continuing.
Except: If the statement inside the try does not execute correctly, then executes the statement inside the except, which can be an error message or other executable statement
ELSE: If the statement inside the try executes normally, then the statement inside the else is executed (equivalent to the program not encountering a fatal error)
Use of try, except, and else to make Python programs more "robust"