Python try except else finally

Source: Internet
Author: User

Python try except else finally instance
classAerror (Exception):"""Aerror---exception"""    Print('Aerror')classBerror (Exception):"""Berror---exception"""    Print('Berror')Try:    #Raise Aerror    #Raise BerrorAAAAA ()exceptAerror:Print("Get Aerror")exceptBerror:Print("Get Berror")except:    Print("Get Exception")Else:    Print("Do Else")finally:    Print("finally")

1. The order in which the try/except/else/finally appears in the complete statement shown above must be try-->except x-->except-->else-->finally, That is, all except must precede else and finally, else (if any) must precede finally, and except x must precede except. Otherwise, a syntax error will occur.

2. In the complete statement above, the presence of the Else statement must be premised on the except X or except statement, and a syntax error is raised if the Else statement is used in a try block without a except statement. That is, else cannot be used in conjunction with try/finally.

3.try success in else, not successfully into the corresponding except;

4. Whether or not successful except X,except judgment statement will be executed, but do not enter; corresponding to the above procedure is Aerror Berror will print in either case, but get Aerror get Berror will print only if the corresponding exception is thrown.

5.finally all cases will be executed, can not write, write must be in all except else, etc.;

Python try except else finally

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.