In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the application technologies related to Python exception handling". In the daily operation, I believe that many people have doubts about the application technology related to Python exception handling. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the application technologies related to Python exception handling?" Next, please follow the editor to study!
Python exception handling has a very powerful processing ability, the error information is displayed to the customer, in the Python exception handling, you can run the operation. The following is a detailed introduction to Python exception handling. I hope you can get something from reading this article.
The exception handling ability of Python is very powerful, and it can accurately feedback error information to users. In Python, an exception is also an object and can be manipulated. All exceptions are members of the base class Exception. All exceptions inherit from the base class Exception and are defined in the exceptions module. Python automatically places all exception names in the built-in namespace, so the program does not have to import the exceptions module to use the exception. Once a SystemExit exception is thrown and no catch is caught, program execution is terminated. If an interactive session encounters an uncaught SystemExit exception, the session terminates.
1. Use try and except statements to catch exceptions
Try: block except [exception, [data …]] : block try: block except [exception, [data...]]: block else: block
The rules for this Python exception handling syntax are:
◆ executes statements under try. If an exception is thrown, the execution process skips to * except statements.
◆ if the exception defined in the * except matches the exception thrown, the statement in the except is executed.
◆ if the exception thrown does not match * except, it will search for the second except, and there is no limit to the number of except allowed to be written.
◆ if all except do not match, the exception is passed to the next layer of try code that calls this code.
◆ executes the else block code if no exception occurs.
Example:
Try: F = open ("file.txt", "r") except IOError, e: print e
The detailed cause of the captured IOError error is placed in object e, and then the except code block of the exception is run
Catch all exceptions
Try: except Exception,ex: print Exception, ":", ex
two。 Use try with finally:
The syntax is as follows:
Try: block finally: block
3. Manually throw an exception with the raise statement:
Raise [exception [, data]]
In Python, the simplest form to throw an exception is to enter the keyword raise, followed by the name of the exception to be thrown. Exception names identify specific classes: Python exception handling is the object of those classes. When the raise statement is executed, Python creates an object of the specified exception class. The raise statement can also specify parameters to initialize the exception object. To do this, add a comma and the specified parameter (or a tuple of parameters) after the name of the exception class.
Example:
Try: raise MyError # throws an exception except MyError: print'an error' raise ValueError,'invalid argument'
The content captured is:
Type = VauleError message = invalid argument
4. Use traceback (tracking) module to view exceptions
Try:
Block
Except:
Traceback.print_exc ()
Example: … Excpetion/traceback.py
5. Using sys module to trace back the exception of *.
Or in the following form:
The return value of import sys tp,val,td = sys.exc_info () sys.exc_info () is a tuple, (type, value/message, traceback)
Exploring the object system of Python
Using Python Standard Library to modify search engine to get results
The use of Python programming in Nginx
Systematic introduction of Python script in VIM
Python handles how text is used in vim
Type here-the type of exception
Value/message-abnormal information or parameters
Traceback-the object that contains the call stack information.
From this point, we can see that this method covers traceback.
6. Some other uses of exception handling
In addition to handling actual error conditions, there are many other uses for Python exception handling. A common use in the standard Python library is to try to import a module and then check to see if it works. Importing a module that does not exist will throw an ImportError exception. You can use this approach to define multiple levels of functionality-depending on which module is valid at run time, or supporting multiple platforms (that is, platform-specific code is separated into different modules).
You can also define your own exception by creating a class that inherits from the built-in Exception class, and then use the raise command to throw your exception
At this point, the study of "what are the application technologies related to Python exception handling" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.