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
- try:
- block
- except [exception,[data…]]:
- block
-
- try:
- block
- except [exception,[data...]]:
- block
- else:
- block
-
-
- try:
-
- f = open(“file.txt”,”r”)
- except IOError, e:
- print e
-
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
- try:
- block
- except [exception,[data…]]:
- block
-
- try:
- block
- except [exception,[data...]]:
- block
- else:
- block
-
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:
- Raise [exception [, data]
-
- Try:
- Raise MyError # throw an exception
- Failed t MyError:
- Print 'a error'
-
- Raise ValueError, 'invalid argument'
-
-
-
4. Use the sys module to trace the final exception
- import sys
- try:
- block
- except:
- info=sys.exc_info()
- print info[0],":",info[1]
-
-
-
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.
- Application manual of Python array in List
- How can I use python to process text in vim?
- Understand how to create a program with multiple threads in Python
- Comparison between python program debugging and c/c ++ Functions
- How to implement language translation using Python translation scripts