Download Chapter 15

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Object-oriented programming wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Control flow wikipedia , lookup

C++ wikipedia , lookup

C Sharp syntax wikipedia , lookup

Structured programming wikipedia , lookup

Transcript
C++ Programming: Program Design Including Data Structures, Fourth Edition
Chapter 15
Exception Handling
At a Glance
Instructor’s Manual Table of Contents

Overview

Objectives

Teaching Tips

Quick Quizzes

Class Discussion Topics

Additional Projects

Additional Resources

Key Terms
15-1
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-2
Lecture Notes
Overview
Chapter 15 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
 See how a try/catch block is used to handle exceptions
 Become familiar with C++ exception classes
 Learn how to create your own exception classes
 Discover how to throw and rethrow an exception
 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
the use of the if statement and the assert statement. Illustrate with examples from
previous chapters.
3. Review situations in previous chapters where exceptions were ignored and the program
was terminated.
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-3
4. Illustrate a typical exception using Example 15-1 (division by zero). Using Examples
15-2 and 15-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
1. Explain the purpose of a try/catch block. Describe how both of these blocks are
processed.
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.
5. Describe the purpose of the throw expression.
6. Examine the syntax of a throw statement and note the rules regarding the
accompanying expression.
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-4
7. Demonstrate the use of the throw statement with Example 15-4.
8. Explain the significance of the ordering of catch blocks.
9. Use Examples 15-5 through 15-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 15-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 15-8 and 159.
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.
Answer: try
2. A try block is followed by one or more ____________________ blocks.
Answer: catch
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-5
3. True or False: If an exception is thrown in a try block, the remaining statements in that
block are executed.
Answer: False
4. A catch block with a(n) ____________________ is designed to catch any type of
exception.
Answer: ellipses
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 15-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.
3. Illustrate how user-defined exceptions are used in a program using Examples 15-11 and
15-12.
4. Demonstrate how to process user-defined exceptions in a function other than main
using Example 15-13.
Rethrowing and Throwing an Exception
1. Explain the circumstances in which rethrowing an exception inside a catch block is
useful.
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
15-14 and 15-15.
Teaching
Tip
Discuss why C++ requires that function headings specify what exceptions can be
thrown in a function.
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-6
Exception Handling Techniques
1. The following sections describe the three choices a programmer has when dealing with a
possible exception in a program.
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
occurs. Note that exceptions involving invalid user input are typically handled with this
method.
2. Examine the program in Example 15-16 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 commonly used
technique for ensuring valid user input.
Log the Error and Continue
1. Describe circumstances in which it is essential that a program continue to run when an
exception has occurred.
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?
Answer: constructors and the function what
2. Constructors with parameters in a user-defined exception class allow users to create their
own ____________________.
Answer: error messages
3. True or False: In C++, all user-defined or built-in exceptions must be derived from the
exception class.
Answer: False
4. True or False: A catch block can rethrow the same exception that it has caught.
Answer: True
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-7
Stack Unwinding
1. Describe how a function-call stack is unwound to propagate an uncaught exception to
the next available try/catch block, or if none is available, to program termination.
2. Use Examples 15-17 and 15-18 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
programs thus far compared to the Web applications they typically use. Visit a popular
Web site and discuss what types of exception handling techniques might be used for
their Web forms.
2. Is there a point beyond which a program might have too much exception handling? If so,
what are the issues that determine how much exception handling to include in a
program?
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 14. Verify that the user has entered valid data, both in
terms of logic and in terms of data types. Test your program with several sets of invalid
data.
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
nested repetition/selection structure then asks what type of item and how much of each
item the user would like to purchase. The loop is exited if the total has exceeded the
user’s budget or if the user enters a sentinel value. Provide exception handling with
try/catch blocks for the user input. In addition, provide a try/catch block to
handle the case in which the budget has been exceeded.
C++ Programming: Program Design Including Data Structures, Fourth Edition
15-8
Additional Resources
1. Exceptions:
www.cplusplus.com/doc/tutorial/exceptions.html
2. Handling Errors Exceptionally Well in C++:
www.cprogramming.com/tutorial/exceptions.html
3. How a C++ Compiler Implements Exception Handling:
www.codeproject.com/cpp/exceptionhandler.asp
4. <stdexcept> members:
http://msdn2.microsoft.com/en-us/library/51yey32d.aspx
Key Terms
 catch block: specifies the type of exception it can catch and contains an exception
handler
 catch block parameter: becomes a placeholder for the value thrown
 class exception: base class of the exception classes provided by C++
 throw statement: generates an exception
 try block: contains statements that may generate an exception