The Effects of an Exception
Explore the impact of exceptions on Python program execution. Understand how raising an exception stops further code, how it propagates through the call stack, and the role of handling exceptions to prevent program termination. Learn about NoReturn type hints and how to interpret tracebacks for effective error management.
We'll cover the following...
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:
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 ...