First, exception handling
In the process of running the program, you will always encounter a variety of errors. Program An error stopped running, then we can not let the program stop running it, this time we need to catch the exception, by catching the exception, we do the corresponding processing.
Let's begin by writing a function that implements the division operation.
def Calc (A, b ): return /print(Calc (5,1))# call, no error, the result is 5.0print(Calc (5,0)) # call again, this time is wrong, because dividend can not be 0, the following reported a pile of errors, the program stopped running >>> zerodivisionerror:division by zero
This time we need to catch the exception, if the divisor is 0, it prompts the user to pass in the wrong parameter, re-incoming.
Catching exceptions using try....except .... keyword, that is, you write code, the first to consider what the code might be wrong, and then do the corresponding processing, try inside is containing the code you do processing, except inside is out of some kind of error, I how to deal with, below we change code, catch exception.
def Calc (b): try : Res = A/b except zerodivisionerror as E:# If the divisor is 0 error, the return divisor cannot be 0, the as E represents the return error information assigned to e return " divisor cannot be 0! " return res# This is if there is no such exception, return the result print (Calc (5,0)) # So again the call will not come out wrong, you can not return the divisor is 0 this.
Above is catching the exception, of course, the above can only catch the divisor of 0 error, if there are other errors can not be caught; there is a finally usage, that is, whether the exception is not out of the execution; Try also has a can catch all the exceptions, and else the use of it is not an exception when the time to do, The following code is used to explain.
defCalc (A, b):Try:#Catching exceptionsres = A/bexceptZerodivisionerror as E:return 'the divisor cannot be 0! ' exceptKeyerror as E:#catch Keyerror This exception, of course, this piece of code is useless to the dictionary, so there is no keyerror this error, I just give an example return 'Keyerror Error' exceptException as E:#This is catching all the anomalies, no matter what abnormalities you have, you can catch them. Print('Other Exceptions') returnE#return error message Else:#this else, which corresponds to the above try, is no exception . Print('no exception') returnResfinally:#This finally is no matter whether this section of code is out of the ordinary go it Print('come out of my way.') Print(Calc (5, 0))#The divisor is abnormal, will return the divisor cannot be 0! and execute the code inside finally Print(Calc (5, 1))#If this is not an exception, it will go inside the other, return the result, and execute the code in the finally. Print(Calc (5,'s'))#This also goes out of the way, the divisor is a string, must not be apart, go to catch all the exception of the code, will also execute the code to execute finally
Second, some common abnormal information:
Attributeerror: Attempted to access a property that an object does not have, such as foo.x, but Foo has no attribute x IOError: input / output exception, generally cannot open file Importerror: Unable to import module or package, Usually a path problem or name error Indentationerror: The code is not aligned correctly and is a syntax error indexerror: The subscript index is out of sequence bounds, such as x has only three elements, but tries to access x[3] Keyerror: Attempting to access a key that does not exist in the dictionary Keyboardinterrupt:ctrl+c is pressed Nameerror: Use a variable that has not been given an object syntaxerror: syntax error TypeError: The incoming object type does not conform to the requirements Unboundlocalerror: an attempt to access a local variable that has not been set is generally due to the fact that there is another variable with the same name outside the code block ValueError: Passing in a value that is not expected by the caller, even if the value is of the correct type
Three, the initiative throws the exception
The initiative throws an exception, that is, we in the code to let it automatically throw an exception, and then capture, for example, when we write automated test script, the results and expectations do not meet, you can actively throw an exception information, and then catch, do other processing, actively throw the exception using the Raise keyword.
Try : Raise Keyerror# actively throws a Keyerror exception except keyerror as E: print(' This is the exception that I volunteered to throw.
Python Learning Note (eight): Exception handling