Section1.10Homework 10 -- Exception Handling and Function Templates
ExercisesExercises
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.
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.
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.
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 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.
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".
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.