Use of try, except, and else to make Python programs more "robust"

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.