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,
softwarefunctions
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.

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".

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.

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.

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.
You have attempted of activities on this page.