In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of sample analysis of errors and exception handling in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Import Exception# except catches error exceptions according to specific error types # multiple different types of exceptions can be caught with one block # EXception can catch all exceptions when the problem or error is uncertain, you can use this kind of print (dir (Exception)) try: # print (b) # capture logic code li= [1line 2 3] print (li [10]) passexcept NameError as msg: # only the errors caught will execute print (msg) passexcept IndexError as msg: print (msg) print ('abnormal first contact') print ('HAHAHAHAHHAHAHAH') here.
Try: # print (b) # Code for capturing logic # li= [1 li [10]) # print (li [10]) print (msg) passexcept IndexError as msg: print (msg) passexcept ZeroDivisionError as msg: print (msg) print ('first contact exception') print ('HAHAHAHAHHAHAHAH')
Exception type
Try: print (b) # Code for capturing logic # li= [1mai 2 3Jere 4] # print (li [10]) # axiom 10Accord0 pass# except NameError as msg:# # will execute # print (msg) # pass# except IndexError as msg:# print (msg) # pass# except ZeroDivisionError as msg:# print (msg) # passexcept Exception as msg: print (msg) passprint ('abnormal first contact') print ('HAHAHAHAHHAHAHAH')
Def A (s): return 10/int (s) passdef B (s): return A (s) * 2def main (): try: B ('0') pass except Exception as msg: print (msg) pass passmain () # does not need to capture in every possible error place As long as the error is caught at the appropriate level, it will greatly reduce the trouble for us to write try----exception # exception throwing mechanism # if the exception interpreter occurs at run time, it will look for the corresponding exception capture type # if it is not found in the current function, it will pass the exception to the upper calling function # if it is not found in the outermost layer The interpreter will exit the program down and drop it.
Class ToolongmyException (Exception): # Custom exception def _ _ init__ (self Leng):'': param leng: length''self.len=leng passdef _ _ str__ (self): return' the data length you entered is'+ str (self.len) + 'has exceeded the length' passdef name_Test (): name=input ('Please enter name:') if len (name) > 5: raise ToolongmyException (len ( Name)) # throw exception else: print (name) pass passname_Test ()
Class ToolongmyException (Exception): # Custom exception def _ _ init__ (self Leng):'': param leng: length''self.len=leng passdef _ _ str__ (self): return' the data length you entered is'+ str (self.len) + 'has exceeded the length' passdef name_Test (): name=input ('Please enter name:') try: if len (name) > 5: Raise ToolongmyException (len (name)) # throws an exception else: print (name) pass except ToolongmyException as result: print (result) pass finally: print ('finished') passname_Test ()
Thank you for reading! This is the end of this article on "sample analysis of errors and exception handling in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.