7. Move the end Automated Test - Tips try ... except ... finally statement

Abnormal Error

When we write code, often meet the program throw the case can not be executed Error

Under normal circumstances, when the Python program can not be processed normally occurs an exception. Python is an exception object representing a mistake. When an exception occurs we need to capture Python script to handle it, otherwise the program will be terminated.

try...except...else

Copy the code
try: 
    normal operating 
   ...................... 
the except: 
    exception occurs, this code is executed 
   .............. ........ 
the else: 
    If no exception execute this block of code
Copy the code

After a fixed except write the name as written except IOError error and we can write multiple statements except

try...except...finally

We can also use the try ... except ... finally ... error handling mechanism   

try ... except ... finally ... and try ... except ... else the difference is whether there was anything wrong finally statement executes

E.g:

Copy the code
try:
    print('try...')
    r = 10 / 0
    print('result:', r)
except ZeroDivisionError as e:
    print('except:', e)
finally:
    print('finally...')
print('END')
Copy the code

When we think some of the code may be wrong, you can use tryto run this code, if executed wrong, then the following code will not continue, but the error-handling code to jump directly to that exceptstatement block, executing the exceptpost, If there is finallya block of statements is executed

Line finallyblock.

Guess you like

Origin www.cnblogs.com/yinlili/p/11313732.html