Created by
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.
Consider the program below, which modifies the BMI program to handle bad user input.
See image.

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.
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.

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.
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.

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.
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.

Students also viewed
Sets found in the same folder
Other sets by this creator
Verified questions
Recommended textbook solutions

Introduction to Algorithms
3rd Edition•ISBN: 9780262033848 (4 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen726 solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th Edition•ISBN: 9780124077263 (3 more)David A. Patterson, John L. Hennessy220 solutions

Operating System Concepts
9th Edition•ISBN: 9781118063330 (2 more)Abraham Silberschatz, Greg Gagne, Peter B. Galvin489 solutions

Starting Out with C++ from Control Structures to Objects
8th Edition•ISBN: 9780133769395Godfrey Muganda, Judy Walters, Tony Gaddis1,294 solutions
Other Quizlet sets
1/6