October 2018
Beginner to intermediate
466 pages
12h 2m
English
When an exception is raised, it appears to stop program execution immediately. Any lines that were supposed to run after the exception is raised are not executed, and unless the exception is dealt with, the program will exit with an error message. Take a look at this basic function:
def no_return():
print("I am about to raise an exception")
raise Exception("This is always raised")
print("This line will never execute")
return "I won't be returned"
If we execute this function, we see that the first print call is executed and then the exception is raised. The second print function call is never executed, nor is the return statement:
>>> no_return() I am about to raise an exception Traceback (most recent call last): ...