Add some of the use of the ITER function: ITER () is actually an iterator, parameters can be passed a list, dict and so on, and then you can call the next function to get the next element, the default does not point to the first element of the object, it can be understood to point to the front of the first element of the position.
8th Lesson: Exception Handling
1, exception handling can improve the fault tolerance of the program
Re = ITER (range (4)) for in range: print re.next () Print ' Hello '
Loop to the 6th time, because Re.next () has no elements returned, there will be stopiteration exceptions. The entire program will be interrupted. when writing a program, you can define a ' contingency plan ' for the type of exception if you know where it might be wrong or what type of error it is.
Re = ITER (range (5))try: in range (+print'Hello'except'here Is end', I
The error-prone part of the try, followed by except, shows that if stopiteration occurs in a try, the except part will be skipped if no exception occurs.
2, the completion of exception processing syntax structure:
Try : except Exception1: except exception2:except: ... Else : finally:
If an exception occurs in a try, the except is executed. Anomaly layer comparison, see if it is Exception1, exception2 ... until it finds its attribution, executes the statement in the corresponding except. If there are no parameters behind the except, then all exception are handed over to this program for processing . Like what:
Try: print (a*2)exceptprint ("TypeError")exceptprint (" Not Type Error & Error noted")
Since a is not defined, it is nameerror. The exception is eventually except: part of the program captures.???
3. If the exception cannot be given to the appropriate object, the exception will continue to be thrown to the upper layer until it is captured or causes the main program to error . such as the following program
def Test_func (): : M = 1/0 except Nameerror: "catch Nameerror in the Sub-function "" try Test_func () Span style= "color: #0000ff;" >except Zerodivisionerror: print ( "catch error in the main program")
Sub-Program try...except ... The structure cannot handle the corresponding error divided by 0, so the error is thrown to the upper master program.
If there is no exception in the try, then the except section skips and executes the statement in the Else .
Finally, there are some things to do, whether or not there is an exception.
The process is as follows
try-> Abnormal->except->finally
Try-> No Abnormal->else->finally
4, throws the exception raise: Actually is oneself writes an unusual
We can also write an example of throwing an exception ourselves:
Stopiteration is a class. When an exception is thrown, it automatically has an intermediate link, which is an object that generates Stopiteration. What Python actually throws is this object. Of course, you can also build your own objects:
Raise Stopiteration ()
In which you want to print out the exception information ' parameter is empty ' direct print E.
Python Basic Learning (7th day)