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
try: normal operating ...................... the except: exception occurs, this code is executed .............. ........ the else: If no exception execute this block of 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:
try: print('try...') r = 10 / 0 print('result:', r) except ZeroDivisionError as e: print('except:', e) finally: print('finally...') print('END')
When we think some of the code may be wrong, you can use try
to run this code, if executed wrong, then the following code will not continue, but the error-handling code to jump directly to that except
statement block, executing the except
post, If there is finally
a block of statements is executed
Line finally
block.