Programming Languages Chapter 14 Programming From Problem Analysis Program Design Eighth Edition Exception Handling Guide

subject Type Homework Help
subject Pages 8
subject Words 2230
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 14-1
Chapter 14
Exception Handling
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching experience
through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings, you will find lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 14-2
Lecture Notes
Overview
Chapter 14 covers the topic of exceptions. After being introduced to exceptions, students
explore various techniques of dealing with exceptions. In particular, the C++
try/catch block and C++ exception classes will be examined. Students will also
learn how to create their own exception classes and throw exceptions. Finally, the
process of stack unwinding as it relates to exceptions will be explained.
Objectives
In this chapter, the student will:
Learn what an exception is
Learn how to handle exceptions within a program
Learn how a try/catch block is used to handle exceptions
Learn how to throw an exception
Become familiar with C++ exception classes and how to use them in a program
Learn how to create your own exception classes
Discover how to throw and rethrow an exception
Explore exception handling techniques
Explore stack unwinding
Teaching Tips
Handling Exceptions within a Program
1. Define an exception and give some simple examples, such as division by zero or out of
bounds access to an array.
Teaching
Tip
Your students will be all too familiar with exceptions by this point. They have
most likely encountered numerous exceptions from input errors and out of range
errors while programming and testing. Ask them to share common errors and/or
exceptions they have found in their programs.
2. Review the ways in which students have dealt with exceptions until this point, including
3. Review situations in previous chapters where exceptions were ignored and the program
page-pf3
14-2 and 14-3, demonstrate how a program might deal with this exception with an if
statement or an assert statement.
Teaching
Tip
Emphasize that exceptions are not limited to cases in which the run-time
environment reacts to improper access or incorrect input with program
termination. Exceptions may also be defined as situations in which something
undesirable happens in terms of program logic. Give some real-world examples
of this from the stock market or transportation industry scenarios presented in
this section.
C++ Mechanisms of Exception Handling
Teaching
Tip
Explain why assert and if statements might not provide adequate exception
handling in an object-oriented application.
1. The following sections will illustrate how to handle exceptions using other C++
mechanisms.
try/catch Block
2. Examine the syntax of a try/catch block.
Teaching
Tip
Verify that students understand how the try block and catch block(s) are
related to each other.
Teaching
Tip
Discuss why C++ allows more than one catch block for each try block.
3. Describe the sequence of events in a try/catch block when an exception occurs.
4. Explain the purpose of the catch block parameter. Note the rules regarding catch
block parameters.
page-pf4
6. Examine the syntax of a throw statement and note the rules regarding the
accompanying expression.
7. Demonstrate the use of the throw statement with Example 14-4.
8. Explain the significance of the ordering of catch blocks.
9. Use Examples 14-5 through 14-7 to demonstrate how the try/catch mechanism
works in a complete program.
Teaching
Tip
This section covered new concepts as well as quite a bit of new terminology.
Step through Example 14-7 in detail to verify that students understand the
try/throw/catch method of handling exceptions in their entirety.
Using C++ Exception Classes
1. Describe the hierarchy of exception classes available in C++. Describe the base class,
exception, and the purpose of its function, what.
Teaching
Tip
Note that most programmers provide additional information about an exception
in the catch block by outputting their own strings along with the exception
object’s what method.
2. Discuss the logic_error and runtime_error classes and explain when each one
is used. Describe some of the classes that are derived from these two classes.
3. Illustrate how C++ exception classes are used in a program with Examples 14-8 and 14-
9.
Teaching
Tip
Note that C++ functions are typically implemented to throw specific exceptions
under certain circumstances. If you are aware of the function prototype, you can
simply catch the exception in your function.
Quick Quiz 1
1. The statements that generate an exception are placed in a(n) ____________________
block.
page-pf5
2. A try block is followed by one or more ____________________ blocks.
3. True or False: If an exception is thrown in a try block, the remaining statements in that
block are executed.
4. A catch block with ____________________ is designed to catch any type of
Creating Your Own Exception Classes
1. Explain that C++ allows programmers to create their own exception classes.
Teaching
Tip
Ask students to think of situations in which they would need to create their own
exception classes.
2. Emphasize that any class in C++ can be an exception class; in other words, it does not
have to be derived from the C++ exception class.
Teaching
Tip
With Example 14-10, note how simple an exception class can be. Explain that
exception classes are primarily used to send information about the exception to
the catch block and therefore do not need to be complex.
14-11 and 14-12.
5. Use Example 14-14 to demonstrate how to throw an exception.
Rethrowing and Throwing an Exception
2. Describe how to rethrow an exception. Note the syntax of the throw expression in the
function heading. Illustrate how to rethrow an exception in a function using Examples
14-15 and 14-16.
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 14-6
Teaching
Tip
Discuss why C++ requires that function headings specify what exceptions can be
thrown in a function.
Exception-Handling Techniques
1. The following sections describe the three choices a programmer has when dealing with a
Terminate the Program
1. Note that in some cases, it is better to simply terminate a program when an exception
occurs. The failure to access an input file is an example of such a case.
Fix the Error and Continue
1. Discuss cases in which it is better to continue program execution after an exception
2. Examine the program in Example 14-17 to illustrate how to continue a program after an
exception.
Teaching
Tip
Explain that using a try/catch block enclosed in a loop is a common
technique for ensuring valid user input.
Log the Error and Continue
2. Explain the advantages of logging errors and analyzing them at a later point.
Quick Quiz 2
1. What members do you typically include in a user-defined exception class?
2. True or False: Constructors with parameters in a user-defined exception class allow
users to create their own error messages.
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 14-7
3. True or False: In C++, all user-defined or built-in exceptions must be derived from the
4. True or False: A catch block can rethrow the same exception that it has caught.
Stack Unwinding
2. Use Examples 14-18 and 14-19 to illustrate how exceptions are propagated in a
program.
Teaching
Tip
Students may not be familiar with stacks yet, so it might be helpful to briefly
describe a stack and its LIFO processing structure. In addition, draw a stack
diagram to accompany the program in this section and write/erase the functions
from the diagram as they are pushed or popped off the stack.
Class Discussion Topics
1. Students may be disconcerted by the gap between the lack of robustness of their
2. Is there a point beyond which a program might have too much exception handling? If so,
3. What are the advantages or disadvantages of creating your own exception class from
scratch as opposed to deriving it from the C++ exception class?
Additional Projects
1. Add exception handling with try/catch blocks to the roommate questionnaire
program you modified for Chapter 13. Verify that the user has entered valid data, both in
page-pf8
2. In Chapter 5, you were asked to write a menu-driven program that calculated the total
price for a lunch. In this program, the user enters his or her budget for the lunch, and a
Additional Resources
1. Exceptions:
3. <stdexcept> members:
Key Terms

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.