Python exception handling (basic)

Source: Internet
Author: User

Python shell >>> open('abc.txt ', 'R') Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'abc.txt 'open a file named abc.txt that does not exist. If the system cannot find the file named abc.txt, an IOError type error will be thrown to us. No such file or directory: abc.txt (there are no files or directories such as abc.txt) Try... else t... if we already know this type of error, we can catch it with an exception. We can use try... retry t to receive this error. Open the file and write it into: try: open ("abc.txt", 'R') failed t IOError: pass, and then run the program, so we can't see any errors, because we have received this IOError using the handle T. Pass indicates that the corresponding implementation is implemented, but nothing is done. What if I output an undefined variable instead of opening a file? Try: print aaexcept IOError: pass obviously, in the above code, I did not assign a value to aa. Run the result: Traceback (most recent call last ): file "/home/fnngj/py_se/tryy. py ", line 3, in <module> print aaNameError: name 'A' is not defined. We have used counter t to receive the error. Why is the error still thrown. If you are careful, you will find that this error type is NameError, And I receive the IOError type. To receive this print error, You need to modify the type of the received error to NameError, I know that the print statement may throw a NameError type error. Although I have received this type error, I do not know the specific error message. So, can I print out the error information? Of course, you can: try: print AAT t NameError, msg: print msg. After receiving the error type, we define a variable msg to receive specific error information, and then print the error information received by msg. Run the program: name 'A' is not defined and only a specific error message is printed. Exception throw mechanism: 1. If an exception occurs during running, the interpreter searches for the corresponding processing Statement (handler ). 2. If it is not found in the current function, it will pass the exception to the upper-layer call function to see if it can be processed. 3. If it is still not found in the outermost layer (global "main"), the interpreter will exit and print traceback to help you find the cause of the error. Note: although most errors may cause exceptions, an exception does not necessarily mean errors. Sometimes they are only a warning, and sometimes they may be a termination signal, such as an exit loop.

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.