Ch. 10 Handling Exceptions (Python3)

Error checking code
Click the card to flip 👆
1 / 12
Terms in this set (12)
Commonly, a program should gracefully handle an exception and continue executing, instead of printing an error message and stopping completely. Code that potentially may produce an exception is placed in a try block. If the code in the try block causes an exception, then the code placed in a following except block is executed.

Consider the program below, which modifies the BMI program to handle bad user input.
See image.
Image: Try and Except
The try and except constructs are used together to implement exception handling, meaning handling exceptional conditions (errors during execution). A programmer could add additional code to do their own exception handling, e.g., checking if every character in the user input string is a digit, but such code would make the original program difficult to read.
A naive approach to adding error-checking code is to intersperse if-else statements throughout the normal code.

Of particular concern is the yellow-highlighted code, which is new branching logic added to the normal code, making the normal code flow of "get weight, get height, then print BMI" harder to see. Furthermore, the second check for negative values before printing the BMI is redundant and ripe for a programming error caused by inconsistency with the earlier checks (e.g., checking for <= here rather than just <).

See image.
Image: Raising exceptions (what not to do)
The following program shows the same error-checking carried out using exception-handling constructs. The normal code is enclosed in a try block. Code that detects an error can execute a raise statement.

The raise statement in the below program causes immediate exit from the try block and the execution of an exception handler. The exception handler prints the argument passed by the raise statement that brought execution there. The key thing to notice is that the normal code flow is not obscured via new if-else statements. You can clearly see that the flow is "get weight, get height, then print BMI".

See image.
Image: Raising exceptions (what to do)
The power of exceptions becomes even more clear when used within functions. If an exception is raised within a function and is not handled within that function, then the function is immediately exited and the calling function is checked for a handler, and so on up the function call hierarchy. The following program illustrates.

Note the clarity of the normal code, which obviously "gets the weight, gets the height, and prints the BMI" - the error checking code does not obscure the normal code.

See image.
Image: Exceptions with Functions
What Finally does1.) If no exception occurs, then execution continues in the finally clause, and then proceeds with the rest of the program. 2.) If a handled exception occurs, then an exception handler executes and then the finally clause. 3.) If an unhandled exception occurs, then the finally clause executes and then the exception is re-raised. 4.) The finally clause also executes if any break, continue, or return statement causes the try block to be exited. See image.Custom exception typesThere are built-in exception types (covered above) and there is also the possibility to create custom exception types. These custom exception types are defined and raised.