The Effects of an Exception

Learn about the effects of exceptions.

NoReturn in exception

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 handled by an except clause, the program will exit with an error message. We’ll examine unhandled exceptions first, and then take a close look at handling exceptions.

Take a look at this basic function:

Press + to interact
from typing import NoReturn
def never_returns() -> NoReturn:
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"

We’ve included the NoReturn type hint for this function. This helps ease mypy’s worry that there’s no way for this function to reach the end and return a string value. The type hint states, formally, the ...