Skip to main content

Section 1.10 Homework 10 -- Exception Handling and Function Templates

Exercises Exercises

1.

One way to think about what an exception means is to imagine that the function throwing the exception returns immediately, without returning a value.Β  This is referred to as a failed function call.
When a function call fails, program execution returns to the location in the program’s code that called the failed function. It is this location that can actually HANDLE the exception thrown by the failed function call.
To indicate programmaticallywhere a failed function should return to for exception handling, use _________________ .
  • a try/catch block
  • an exception specification list
  • stack unwinding
  • a custom exception class

2.

True, or false:Β  When an exception occurs during program execution, there is considerable overhead in the run-time environment.
  • true
  • false

3.

4.

There may be multiple kinds of exceptions, each from a different exception class. This allows you to write multiple catch blocks, each handling a different kind of failure.
Which of the following is the CORRECT way to create a "catch all" block to handle any type of unhandled exception?
  • catch ( ... )Β Β {Β // exception handling code here }
  • catch ( )Β Β {Β // exception handling code here }
  • catch ( all )Β Β {Β // exception handling code here }
  • catch ( * )Β Β {Β // exception handling code here }

5.

6.

Which of the following is FALSE?
  • In the real word, error handling plays only a minor role in the time it takes to develop large software projects.
  • Exceptions are a "clean" way of reporting errors encountered during program execution.
  • You should only use exceptions for error situations, not for normal control flow of your program.
  • When an exception is thrown by a function, the execution of that function is halted.

7.

Suppose that while your program is executing, an exception is thrown for which your code does not provide a handler.Β  What happens?
  • The program execution ends in a "crash", caused by a system call to terminate().
  • The compiler-supplied "default handler" kicks in to handle the situation gracefully.
  • The program continues running, picking up from the line of code following the failed function call.

8.

True, or false:Β  Function failableFunction has the ability to throw an exception. If function myFun calls failableFunction, then myFun is REQUIRED by the compiler to handle any resulting exceptions; failure to provide a handler for each possible thrown exception will result in a compile-time error.
  • false
  • true

9.

According to techterms.com,
...in the world of software development, "deprecated" refers to functions or elements that are in the process of being replaced by newer ones. The term comes from the verb "to deprecate," which means to disapprove of something. While deprecated items may work in the current version of a programming language, they may not function in future updates. Therefore, software developers are advised to replace deprecated items with other options.
Which of the following is deprecated in C++11?
  • exception specifications, such as including "throw(FileNotFoundException)" as part of a function protoype.
  • try/catch blocks
  • exception handling
  • the standard library exception class

10.

11.

We have defined a function, mystery(int a, int b), that takes two integers, `a` and `b`, and returns a mysterious string result.Β  Sometimes the function may fail and throw an exception.
Write a second function, myFun(int a, int b), that does the following:
- Call mystery(a, b).
- If the call results in a normal return, return that result.
- If the call results in a domain_error exception, return the exception’s "what" message.
- If the call results in any other kind of exception, return the string "FAILURE".
Use a try-catch block to help you solve this problem.
For example:
Test Result
cout << myFun(0, 1);
cout << myFun(4, 5);
I don't like those numbers
cout << myFun(5, 5);
FAILURE

12.

Write a function array_sum that takes two arguments: an integer array called ’nums’ and an integer called ’size’ representing the number of values stored in the array. The function accumulates the sum of the values in the ’nums’ array and returns that sum.
If size < 1, the function throws a domain_error exception with the message, "size < 1 is not allowed".
For example:
Test Result
int arr[1] = {3};
int sum;
try {
    sum = array_sum(arr, 1);
    cout << sum;
}
catch(...) {
    cout << "Your code threw an exception." << endl;
}
int arr[5] = {1, 2, 3, 4, 5};
int sum;
try {
    sum = array_sum(arr, -4);
    cout << sum;
}
catch(domain_error e) {
    cout << e.what() << endl;
}
catch(...) {
    cout << "wrong exception type" << endl;
}
size < 1 is not allowed
int arr[5] = {1, 2, 3, 4, 5};
int sum;
try {
    sum = array_sum(arr, 5);
    cout << sum;
}
catch(domain_error e) {
    cout << e.what() << endl;
}
catch(...) {
    cout << "wrong exception type" << endl;
}

13.

Write a function template compare that takes two values of the same type and prints the message "Those are the same" if the items are equal; if they are unequal, the function prints the message "Those are different".
This template should work for any type that can use the "==" operator and the copy constructor.
NOTE: The compare template has a void return type.
For example:
Test Result
compare(3, 7);
Those are different
compare("blueberry", "blueberry");
Those are the same

14.

Write a function template swapper that takes two values passed by reference and swaps them.
NOTE: The swapper template has a void return type.
For example:
Test Result
int x = 3;
int y = 7;
swapper(x, y);
cout < "x = " < x < " and y = " < y < endl;
x = 7 and y = 3
string a = "raspberry";
string b = "preserves";
swapper(a, b);
cout < "a = " < a < " and b = " < b < endl;
a = preserves and b = raspberry

15.

Write a functionΒ template maximum that takes two values of the same type and returns the larger value.Β  This template should work for any type that can use the ">" operator and the copy constructor.
For example:
Test Result
int x = 3;
int y = 7;
cout << "The maximum is " << maximum(x, y) << endl;
The maximum is 7
string a = "raspberry";
string b = "jelly";
cout << "The maximum is " << maximum(a, b) << endl;
The maximum is 7
You have attempted of activities on this page.