Python 37th days -- exception -- socket, 37th days -- socket
Exception Handling
1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 5 list_l = ['G ', 'H'] 6 data = {'1': '3'} 7 try: 8 # list_l [3] # IndexError: list index out of range 9 data ['2q'] 10 bytes t IndexError as e: # The list subscript exceeds the boundary 11 print (e) 12 bytes t KeyError as e: # dictionary key does not exist 13 print ('Do not have this key', e) 14 parse t Exception as e: # most exceptions can capture 15 print (e) 16 else: # use 17 print ('above normal') 18 finally: 19 print ('print at last ')
Exception type:
''''' Common exception AttributeError attempts to access a tree without an object, such as foo. x, but foo has no attribute xIOError input/output exception. Basically, the file ImportError cannot be opened and modules or packages cannot be introduced; basically, it is a path problem or name error. IndentationError syntax error (subclass). The Code does not correctly align the IndexError subscript index beyond the sequence boundary. For example, if x has only three elements, however, I tried to access x [5] KeyError. I tried to access the KeyboardInterrupt Ctrl + C key that does not exist in the dictionary. I was pressed NameError and used the SyntaxError Python code, which is invalid, the Code cannot compile TypeError to pass in the object type and the required non-conforming UnboundLocalError attempts to access a local variable that has not been set, basically because there is another global variable with the same name, as a result, you think that you are accessing its ValueError and passing in a value that the caller does not expect, even if the value type is correct '''''
Custom exception
1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 5 class NewException (Exception ): # inherit the basic class exception 6''' custom exception information ''' 7 def _ init _ (self, msg): 8 self. message = msg 9 10 def _ str _ (self): # not defined. The base class has 11 # return self. message # return exception information 12 return 'triggers a custom exception ...... '#13 14 try: 15 raise NewException ('triggering custom exception') 16 TB t NewException as e: 17 print (e)
Socket Communication