Use Python exception handling to Display error messages

Source: Internet
Author: User

Python exception handling has powerful processing capabilities. The error information is displayed to the customer. in Python exception handling, you can operate on it, the following is a detailed introduction to Python exception handling. Python has powerful exception handling capabilities and can accurately report error information to users.

1. Use try and try t statements to capture exceptions

 
 
  1. try:  
  2.    block  
  3. except [exception,[data…]]:  
  4.    block  
  5.  
  6. try:  
  7. block  
  8. except [exception,[data...]]:  
  9.    block  
  10. else:  
  11.    block  
  12.  
  13.  
  14. try:  
  15.  
  16.    f = open(“file.txt”,”r”)  
  17. except IOError, e:  
  18.    print e  
  19.  

Python exception handling has powerful processing capabilities. The error information is displayed to the customer. in Python exception handling, you can operate on it, the following is a detailed introduction to Python exception handling.

Python has powerful exception handling capabilities and can accurately report error information to users.

2. Use try and try t statements to capture exceptions

 
 
  1. try:  
  2.    block  
  3. except [exception,[data…]]:  
  4.    block  
  5.  
  6. try:  
  7. block  
  8. except [exception,[data...]]:  
  9.    block  
  10. else:  
  11.    block  
  12.  

The detailed cause of the captured IOError will be placed in Object e, and then the mongot code block that runs this python exception handling will capture all the exceptions.

3. Manually raise an exception using the raise statement:

 
 
  1. Raise [exception [, data]
  2.  
  3. Try:
  4. Raise MyError # throw an exception
  5. Failed t MyError:
  6. Print 'a error'
  7.  
  8. Raise ValueError, 'invalid argument'
  9.  
  10.  
  11.  

4. Use the sys module to trace the final exception

 
 
  1. import sys  
  2. try:  
  3.    block  
  4. except:  
  5.    info=sys.exc_info()  
  6.    print info[0],":",info[1]  
  7.  
  8.  
  9.  

5. other usage of python exception handling

The following example demonstrates how to use exceptions to support specific platform functions. The Code comes from the getpass module, a encapsulation module that obtains the password from the user. The implementation of the obtained password on UNIX, Windows, and Mac OS platforms is different, but this Code encapsulates all the differences.

  1. Application manual of Python array in List
  2. How can I use python to process text in vim?
  3. Understand how to create a program with multiple threads in Python
  4. Comparison between python program debugging and c/c ++ Functions
  5. How to implement language translation using Python translation scripts

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.