Chapter Questions
What is a throw point?
What is an exception handler?
Explain the difference between a try block and a catch block.
What happens if an exception is thrown, but not caught?
What is “unwinding the stack”?
What happens if an exception is thrown by a class’s member function?
How do you prevent a program from halting when the new operator fails to allocate memory?
Why is it more convenient to write a function template than a series of overloaded functions?
Why must you be careful when writing a function template that uses operators such as [] with its parameters?
The line containing a throw statement is known as the __________.
The __________ block contains code that directly or indirectly might cause an exception to be thrown.
The __________ block handles an exception.
When writing function or class templates, you use a(n) __________ to specify a generic data type.
The beginning of a template is marked by a(n) __________.
When defining objects of class templates, the __________ you wish to pass into the type parameter must be specified.
A(n) __________ template works with a specific data type.
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.
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.
Make the function you wrote in Question 17 a template.
Write a template for a function that displays the contents of an array of any type.
True or FalseT F There can be only one catch block in a program.
True or FalseT F When an exception is thrown, but not caught, the program ignores the error.
True or FalseT F Data may be passed with an exception by storing it in members of an exception class.
True or FalseT F Once an exception has been thrown, it is not possible for the program to jump back to the throw point.
True or FalseT F All type parameters defined in a function template must appear at least once in the function parameter list.
True or FalseT F The compiler creates an instance of a function template in memory as soon as it encounters the template.
True or FalseT F A class object passed to a function template must overload any operators used on the class object by the template.
True or FalseT F Only one generic type may be used with a template.
True or FalseT F In the function template definition, it is not necessary to use each type parameter declared in the template prefix.
True or FalseT F It is possible to overload two function templates.
True or FalseT F It is possible to overload a function template and an ordinary (nontemplate) function.
True or FalseT F A class template may not be derived from another class template.
True or FalseT F A class template may not be used as a base class.
True or FalseT F Specialized templates work with a specific data type.
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;}
try{quotient = divide(num1, num2);}cout << "The quotient is " << quotient << endl;catch (string exceptionString){cout << exceptionString;}
template <typename T>T square(T number){return T * T;}
template <typename T>int square(int number){return number * number;}
template <typename T1, typename T2>T1 sum(T1 x, T1 y){return x + y;}
Assume the following definition appears in a program that uses the SimpleVector class template presented in this chapter.int <SimpleVector> array(25);
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;