• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • Exceptions and Templates

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 16

Exceptions and Templates - all with Video Answers

Educators


Chapter Questions

Problem 1

What is a throw point?

Check back soon!

Problem 2

What is an exception handler?

Check back soon!

Problem 3

Explain the difference between a try block and a catch block.

Check back soon!

Problem 4

What happens if an exception is thrown, but not caught?

Check back soon!

Problem 5

What is “unwinding the stack”?

Check back soon!

Problem 6

What happens if an exception is thrown by a class’s member function?

Check back soon!

Problem 7

How do you prevent a program from halting when the new operator fails to allocate memory?

Check back soon!

Problem 8

Why is it more convenient to write a function template than a series of overloaded functions?

Check back soon!

Problem 9

Why must you be careful when writing a function template that uses operators such as [] with its parameters?

Check back soon!

Problem 10

The line containing a throw statement is known as the __________.

Check back soon!

Problem 11

The __________ block contains code that directly or indirectly might cause an exception to be thrown.

Check back soon!

Problem 12

The __________ block handles an exception.

Check back soon!

Problem 13

When writing function or class templates, you use a(n) __________ to specify a generic data type.

Check back soon!

Problem 14

The beginning of a template is marked by a(n) __________.

Check back soon!

Problem 15

When defining objects of class templates, the __________ you wish to pass into the type parameter must be specified.

Check back soon!

Problem 16

A(n) __________ template works with a specific data type.

Check back soon!

Problem 17

Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception.

Check back soon!

Problem 18

Write a function that dynamically allocates a block of memory and returns a char pointer to the block. The function should take an integer argument that is the amount of memory to be allocated. If the new operator cannot allocate the memory, the function should return a null pointer.

Check back soon!

Problem 19

Make the function you wrote in Question 17 a template.

Check back soon!

Problem 20

Write a template for a function that displays the contents of an array of any type.

Check back soon!

Problem 21

True or False
T F There can be only one catch block in a program.

Check back soon!

Problem 22

True or False
T F When an exception is thrown, but not caught, the program ignores the error.

Check back soon!

Problem 23

True or False
T F Data may be passed with an exception by storing it in members of an exception class.

Check back soon!

Problem 24

True or False
T F Once an exception has been thrown, it is not possible for the program to jump back to the throw point.

Check back soon!

Problem 25

True or False
T F All type parameters defined in a function template must appear at least once in the function parameter list.

Check back soon!

Problem 26

True or False
T F The compiler creates an instance of a function template in memory as soon as it encounters the template.

Check back soon!

Problem 27

True or False
T F A class object passed to a function template must overload any operators used on the class object by the template.

Check back soon!

Problem 28

True or False
T F Only one generic type may be used with a template.

Check back soon!

Problem 29

True or False
T F In the function template definition, it is not necessary to use each type parameter declared in the template prefix.

Check back soon!

Problem 30

True or False
T F It is possible to overload two function templates.

Check back soon!

Problem 31

True or False
T F It is possible to overload a function template and an ordinary (nontemplate) function.

Check back soon!

Problem 32

True or False
T F A class template may not be derived from another class template.

Check back soon!

Problem 33

True or False
T F A class template may not be used as a base class.

Check back soon!

Problem 34

True or False
T F Specialized templates work with a specific data type.

Check back soon!

Problem 35

Each of the following declarations or code segments has errors. Locate as many as possible.

catch
{
quotient = divide(num1, num2);
cout << "The quotient is " << quotient << endl;
}
try (string exceptionString)
{
cout << exceptionString;
}

Check back soon!

Problem 36

Each of the following declarations or code segments has errors. Locate as many as possible.

try
{
quotient = divide(num1, num2);
}
cout << "The quotient is " << quotient << endl;
catch (string exceptionString)
{
cout << exceptionString;
}

Check back soon!

Problem 37

Each of the following declarations or code segments has errors. Locate as many as possible.

template <typename T>
T square(T number)
{
return T * T;
}

Check back soon!

Problem 38

Each of the following declarations or code segments has errors. Locate as many as possible.

template <typename T>
int square(int number)
{
return number * number;
}

Check back soon!

Problem 39

Each of the following declarations or code segments has errors. Locate as many as possible.

template <typename T1, typename T2>
T1 sum(T1 x, T1 y)
{
return x + y;
}

Check back soon!

Problem 40

Assume the following definition appears in a program that uses the SimpleVector class template presented in this chapter.
int <SimpleVector> array(25);

Check back soon!

Problem 41

Assume the following statement appears in a program that has defined valueSet as an object of the SimpleVector class presented in this chapter. Assume valueSet is a vector of ints, and has 20 elements.
cout << valueSet<int>[2] << endl;

Check back soon!