In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to deal with python abnormal propagation". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to deal with python abnormal propagation.
1. Abnormal propagation
When an exception occurs in a function, if the exception is handled in the function, the exception will not continue to propagate. If the exception is not handled in the function, the exception continues to propagate to the function caller. If the function caller handles the exception, it no longer propagates, and if it has not, it continues to propagate to his caller until it is passed to the global scope (main module). If it is still not handled, the program terminates and the exception message is displayed to the console. So the propagation of an exception is also called throwing an exception.
Examples of exception propagation are as follows:
Def fn1 (): print ('Hello fn') print (10 Hello fn3' 0) def fn2 (): print (' Hello fn2') fn () def fn3 (): print ('Hello fn3') fn2 () fn3 ()
Output result:
Explain the output result:
When we call the fn3 () method, we output the Hello fn3 first, then call the fn2 () method down, and so on into the fn1 () method. You can see that there are three output statements above.
When a ZeroDivisionError exception occurs when the print (10swap 0) statement in the fn1 () method is executed, but the exception is not handled in the fn1 () method, the exception is thrown to his caller and thrown to the fn2 () method, and so on, knowing that the fn3 () method is thrown to the caller in the global scope, and the exception is still not handled.
Finally, the Python interpreter displays the exception in the console.
So we see that in the abnormal result, one line reports an error and throws it up several times.
For example, if the code in the global scope has an exception that is not handled, it will be displayed directly in the console.
Print (10 / 0)
Output result:
2. How to handle exceptions
Any caller after the exception is thrown can handle the exception.
The exception handling is as follows:
Def fn1 (): print ('Hello fn') print (10 / 0) def fn2 (): print (' Hello fn2') fn1 () def fn3 (): print ('Hello fn3') fn2 () try: fn3 () except: print ("I have handled the exception.") "output result: I have handled the Hello fn3Hello fn2Hello fn exception." # I did exception handling in the global scope, and # can handle it elsewhere, no matter where it is handled, # will not throw the error to the console in the end.
Tip:
When an exception occurs during the running of the program, all the exception information will be saved in a special exception object. When the exception propagates, it actually throws the exception object to the caller.
Different errors correspond to different exception objects (that is, the exception types mentioned above).
How do I view Python exception objects?
In the local documentation of Python, find [The Python Standard Library]-> [Built-in Exceptions], which is full of exception objects and instructions in our Python.
As shown in the following figure:
Thank you for your reading, the above is the content of "how to deal with python abnormal propagation". After the study of this article, I believe you have a deeper understanding of how to deal with python abnormal propagation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.