Exception Handling and Network Protocols

An exception handling:

     1 Exception: a signal that an error occurs, once the program fails, there is no corresponding processing mechanism in the program, an exception will be thrown and the program will be terminated

  Three parts of an exception:

            trace information,
            exception value,
            exception type,

Two types of exceptions:
        1 Syntax exception:
               This type of exception is corrected before the program runs, otherwise the program cannot run.
        2 Logical exceptions: Common exception types:

Indexerror,Keyerror,Attributeerror,
FileNotFoundError,
Valueerror,Typeerror,IndexError
Nameerror: name is not defined

 

l=【1,2
l[ 23424 ] search out of range
 4  try :
print('start..')
x=1
and
l=[]
l=[3]
print('=====')
except NameError: determine whether the error is a NameError,
print( ' name is not defined ' )
except KeyError:

 

There can be multiple except judgments:
after the except match is successful, the code after the except is executed,
then it will be very long to write in this way, so we have another method:
except (NameError, KeyError): there is an or relationship.
print('variable name or dictionary key,')

5 Universal exception:

try
。。。
;;;
。。。
except Exception: Universal exception, can handle all exceptions, but if you want to see exception information, you need to use it with as, assign it to any variable, and print out the variable.
except Exception:
print('Universal exception!!!') At this time, you don't know what the exception is, so we need to use an as syntax.
except Exception as e: Use the as syntax to assign it to a value, and then print it out to see the exception information.
print('Universal exception------"', e)
You want to use a logic to handle all exceptions, then use universal exceptions,

 

6 try:. . . with else:. . . Use in conjunction.

try
,,,,,
;;;
。。。
else : else cannot be used alone, it must be used with except Exception:
print('Universal exception!!!') At this time, you don't know what the exception is, so we need to use an as syntax.
except Exception as e: Use the as syntax to assign it to a value, and then print it out to see the exception information.
print('Universal exception------"
After these, use together.
else:
print('The detected code block, the sub-code block of else will be executed without any exception')

 

7 try。。。finally。。。

finally : useful. . . Can put and recycle system resources f.close
print('It will run whether there is an exception or not, and it will be executed whether the exception is handled or not')
try
f=open('a.txt','w',encoding='utf-8')
f.read()
f.close
exception NameError
print( ' name is wrong ' )
In this case, when our file is opened for writing, the read operation cannot be performed.
So it will report an error, the program ends, then the file f.close does not run, so
At this time, finally is used, and the operation of closing the file is placed under finally.

 

8 Actively trigger an exception:
raise TypError ('type error content'), where TypeError is the class.

9 Assert:

print('part1.....')if len ( 
stus )    
 = < 0 _ _ _ _ _ _ _ _ _ _
raise TypError( ' The number of values ​​in stus must be greater than 0, the subsequent code will run ' )
Can I replace the if and the code above with one line?
assert len(stus) > 0 asserts that the length of stus is greater than 0, if the program continues to run, otherwise throws an error,

print('part2,,,,')
print(',,,,')
print(',,,,')
print(',,,,')
print(',,,,')

 

10 Custom exceptions:

class RegisterError(BaseException):继承BaseException
def __init__(self,msg,user):
self.msg=msg
self.user=user
def __str__(self):
return ('%s %s'%(self.msg,self.user))
raise RegisterError('Registration failed', 'teacher')
Customize your own exception, you can customize the information thrown.

 

11 When to use exceptions:
When the conditions of your error are predictable, you can directly use if to judge to solve it.
When the error occurs, it is impossible to predict, but it will definitely appear, you can only use try:excep. . .

 

Three socketer network programming:

C/S architecture: client<--------->server

B/S architecture: browser<---------->server

Learning socket programming is to write a client software and server software.

Then realize the communication between the server and the client based on the network.

1 What is a network:

      1 Physical connection medium

      2 Internet Protocol:

          The Internet Protocol is a unified standard, and the Internet Protocol is like the English of the computer world.

Four network protocols:

Reference: http://www.cnblogs.com/linhaifeng/articles/5937962.html#_label4

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613332&siteId=291194637