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--
Today, the editor will share with you the relevant knowledge points about how to avoid the pit in Python exception handling. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Execution order of finally and return
The sample code is as follows:
Class Test (object): def division (self, num1) Num2): try: if num2 = 0: return 'divisor cannot be 0' avg = num1 / num2 return avg except Exception as e: print ('ERROR') finally: print (' FINALLY') if _ _ name__ = ='_ main__': test = Test () print (test.division (6) 0)) # > the execution result is as follows: # > FINALLY# > Divisor cannot be 0
Note: even if there is return, finally will be executed, and it will be executed before return. So the end result is.
Execution order of else and return
The sample code is as follows:
Class Test (object): def division (self, num1, num2): try: avg = num1 / num2 return avg except Exception as e: print ('ERROR') else: print (' No ERROR') finally: print ('FINALLY') if _ _ name__ =' _ main__': test = Test () print (test.division (6) 2)) # > execution result is as follows: # > FINALLY# > > 3.0
Note: else is not executed when there is return. So the result of the execution is like the sample code above
So what is the result of execution without return?
Class Test (object): def division (self, num1) Num2): try: avg = num1 / num2 # return avg except Exception as e: print ('ERROR') else: print (' No ERROR') finally: print ('FINALLY') if _ _ name__ =' _ main__': test = Test () print (test.division (6) 2)) # > execution result is as follows: # > No ERROR# > FINALLY# > None
From the running results, we draw a conclusion: when there is no return, else is executed normally.
If an exception occurs, else will not be executed. The code example is as follows:
Class Test (object): def division (self, num1, num2): try: avg = num1 / num2 return avg except Exception as e: print ('ERROR') else: print (' No ERROR') finally: print ('FINALLY') if _ _ name__ =' _ main__': test = Test () print (test.division (4) 0) # > execution results are as follows: # > ERROR# > FINALLY# > above None is all the contents of the article "how to avoid pitfalls in Python exception handling" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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: 245
*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.