Python Exception handling

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-Try: Open ("Xxx.txt")    #print (num)    Print('test1 ....')    #print (num)exceptNameerror:Print('there is an exception that is being processed ...')exceptFilenotfounderror:Print('No such file or directory')Print('test2 ....') Results: No such fileordirectorytest2 ....

You can also merge writes:

#!/usr/bin/env python#-*-coding:utf-8-*-Try: Open ("Xxx.txt")    #print (num)    Print('test1 ....')    #print (num)except(Nameerror,filenotfounderror):##这是python3的写法    Print('there is an exception that is being processed ...')

#exceptNameerror,filenotfounderror:## #这是python2的写法Print('test2 ....') Result: There is an exception that is being processed ... test2 ....

Exception all the notation:

#!/usr/bin/env python#-*-coding:utf-8-*-Try:    11/0#Open ("Xxx.txt")    #print (num)    Print('test1 ....')except(nameerror,filenotfounderror):Print('If you catch an exception after you do the processing ...')exceptException:##这是python3 The method of capturing all exceptions, Python2 writes except directly:    Print('If exception is used, it means that the above except does not catch an exception, and this except is bound to capture')#except Exception as RET:#print (' If exception is used, it means that the above except does not catch an exception, this except will catch ')#print (ret)Else:    Print('features that do not have an exception')finally:    Print('whether there is an exception, the last function to perform')Print('test2 ....')

Results:

If exception is used, it means that the above except does not catch an exception, and this except is bound to capture
Whether there is an exception, the last function to perform
Test2 ....

Python Exception handling

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.