Exception handling is essential in project development. Exception handling helps people debug and makes it easier to find bugs by being more informative. Exception handling also improves the fault tolerance of the program.
When we talked about loop objects earlier, we mentioned a StopIteration exception, which is an error that is reported when a loop object exhausts all its elements.
Let's use it as an example of basic exception handling.
A program that contains exceptions: the
re = iter(range(5))
for i in range(100):
print ()
print 'HaHaHaHa'
First, we define a loop object, re, that will make 5 loops, each using one element of the sequence.
In subsequent for loops, we manually call the next() function. When the loop reaches the 6th time, instead of returning the element, () will throw a (raise)StopIteration exception. The entire program will break.
We can modify the above exception program until it is perfect and bug free, but on the other hand, if we know when we write the program that we may make a mistake here and the type of mistake we may make, we can define a "contingency plan" for that exception type.
re = iter(range(5))
try:
for i in range(100):
print ()
except StopIteration:
print 'here is end ',i
print 'HaHaHaHa'
In the try section of the program, we put in the error-prone part. We can follow up with an except to show what the program should do if a StopIteration occurs in the statement in the try section. If no exception occurs, the except section is skipped.
The program will then continue to run instead of being completely interrupted.
The complete syntax structure is as follows:
try:
...
except exception1:
...
except exception2:
...
except:
...
else:
...
finally:
...
If an exception occurs in the try, the attribution of the exception is executed, and the except is executed. the exceptions are compared layer by layer to see if they are exception1, exception2..., until the attribution is found and the corresponding statement in the except is executed. ..., until it finds the attribution and executes the corresponding statement in the except. If except is not followed by any arguments, then all exceptions are handed off to this program. For example.
try:
print(a*2)
except TypeError:
print("TypeError")
except:
print("Not Type Error & Error noted")
Since a is not defined, it is a NameError. the exception is eventually caught by the except: part of the program.
If the exception cannot be handed off to the appropriate object, the exception will continue to be thrown up the hierarchy until it is caught or causes the main program to report an error. For example, the following program
def test_func():
try:
m = 1/0
except NameError:
print("Catch NameError in the sub-function")
try:
test_func()
except ZeroDivisionError:
print("Catch error in the main program")
Subroutine try... .except... structure of the subroutine cannot handle the corresponding divide-by-0 error, so the error is thrown to the main program above.
If there are no exceptions in the try, then the except section is skipped and the statement in the else is executed.
FINALLY are some of the things that end up being done whether there is an anomaly or not.
The process is as follows.
try->exception->except->finally
try->no exception->else->finally
throw an exception
We can also write our own example of throwing an exception: the
print 'Lalala'
raise StopIteration
print 'Hahaha'
This example doesn't have any practical significance. It's just to illustrate what the RAISE statement does.
StopIteration is a class. When an exception is thrown, there is an automatic intermediate step of generating an object for StopIteration, which is what Python actually throws. Python actually throws this object. Of course, you can also generate your own object.
raise StopIteration()
summarize
try: ... except exception: ... else: ... finally: ...
raise exception