python--write their own anomalies (to form a powerful capture and processing power)

Source: Internet
Author: User

"" "1, when writing their own exceptions, is usually inherited Exception2, if inherited is baseexception, then will not be the general except exception catch to 3, their own definition of the exception Python will not automatically trigger, Need to trigger yourself, trigger with raise 4, Python will only automatically trigger its own built-in exceptions, such as Indexeerror,nameerror,ioerror, as long as the error end is usually built-in exception "" "class  MyException (Exception):     def __init__ (self, msg):  # The MSG parameter is used to receive the error description information that triggers the exception simultaneous come in         self.msg = msg     def __str__ (self):  #格式化输出         return  "[ internal logic error:] %s " %  (self.msg) #例子1:# if __name__ ==  ' __ Main__ ':#     flag = false#     try:#          if flag:#              print ("Initialization data") #              print ("Done ...") #         else:#              raise myexception ("No data")   #触发自己的异常 #      except MyException as err:  #捕捉被触发的自定义异常 #          print (Err)   #打印异常信息, if you have parameters, you can print the parameter by Err.args # example 2: Can I catch a built-in exception in a custom exception? # if __name__ ==  ' __main__ ':#     a = [1,2,3,4]#      # print (a[10])  #  put this sentence here, then throw Indexerror: list index out  of range, and the program stops running #     flag = false#      try:#         if flag:#              print ("Initialization data") #              prinT ("done ...") #             print (a[10])  # Here to access an incorrect index that seems to catch no #         else:#              raise myexception ("No data")   #触发自己的异常 #      except MyException as err:  #捕捉被触发的自定义异常 #          print (Err)   #打印异常信息, if you have parameters, you can print parameters by Err.args #****************** Write a custom exception class that requires a given initial value ****************class loginexce (Exception):     def __init__ ( Self, username, password):         super (). __init__ ("Your  username:{name} and passwd:{pwd} is error ". Format (Name=username, pwd=password))         self.user = username         self.pwd =&nbsP;password    def log (self):         print (" Log exception logging to log ... ")         log_err_info =  (" u:%s p:%s  Login failed " %  (self.user, self.pwd)) # if __name__ ==  ' __main__ ': #      auth = False  #这标志位用于模拟登录的状态 #     try:#          if auth:#              print ("Login Successful ...") #          Else:#             raise loginexce ("Toby ", " [email protected] ")   #如登录失败则引发异常 #     except loginexce as  err:  #捕捉这个异常 #         err.log ()   #调用异常类的日志记录的方法 #           print (Err)   #打印异常信息 ##      According to the above test, only after the custom exception has been raised, To access an instance of the exception, called err# see the following two functions, one with if, and one by catching the exception def func_a (num):    try:         print (100 / num)     except  ( Zerodivisionerror, typeerror):         print ("Value Value  can not be illegal ") Def func_b (num):     if num ==  0:        print ("Value value can not be  0 ")         return    if isinstance (num,  STR):         print ("Value value can not be  str ")         return    else:         print (100&Nbsp;/ num) func_a (0) func_b (0) func_a ("abc")   #传入字符串func_b ("abc")   #传入字符串 # Well, actually these two ways can be used together, Powerful capture and processing power


python--write their own anomalies (to form a powerful capture and processing power)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.