In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 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 keywords and structure of Python exception handling". In daily operation, I believe that many people have doubts about the keywords and structure of Python exception handling. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the keywords and structure of Python exception handling?" Next, please follow the editor to study!
1. Grammatical format
Let's start with the abnormal syntax format of Python.
From the image above, you can see the following information:
Four key words: try, except, else, finally
Support multiple anomaly monitoring.
Among them
The try block contains code that may throw an exception
Except blocks are used to catch and handle exceptions that occur.
If no exception is thrown in the try block, the else block is executed
The finally block is executed regardless of whether an exception occurs; it is usually used to release the resources requested in the try block.
We always know that in the program code, some keywords are necessary and some are non-essential. According to the above grammatical format, I can't ask myself: can there be multiple combinations?
2.try... An except structure
Or an exception catch case in which test 0 cannot be divisor. The specific code and execution effect are shown below:
Code logic and execution results:
Receive the digits entered by the keyboard as divisor and divisor
Two print statements are used in the try module to verify whether division is performed.
The subsequent print statement is not executed because z = x / y throws an exception
The except module calls the system exception BaseException in Python to print out the exception information.
3.try... Multiple except structures
In view of the above cases, we can further catch more detailed exceptions. As shown below:
4.try...except...else junction
Furthermore, after the normal execution of try, the execution of some statement blocks is controlled by else. See the following figure:
5.try...except...finally structure
In the end, you will always encounter blocks of statements that will be executed regardless of the result of the execution of tyr or Except, which is useful for post-finally. See the following figure:
At this point, the study of "what are the keywords and structures of 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.