• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/21

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

21 Cards in this Set

  • Front
  • Back
1
1
1
1
1. What output is produced by the following code?
int waitTime = 46;
try
{
cout << "Try block entered.\n";
if (waitTime > 30)
throw waitTime;
cout << "Leaving try block.\n";
}
catch(int thrownValue)
{
cout << "Exception thrown with\n"
<< "waitTime equal to " << thrownValue << endl;
}
cout << "After catch block" << endl;
1. Try block entered.
Exception thrown with
waitTime equal to 46
After catch block.
2. What would be the output produced by the code in Self-Test Exercise 1 if we made the
following change? Change the line
int waitTime = 46;
to
int waitTime = 12;
2. Try block entered.
Leaving try block.
After catch block.
3. In the code given in Self-Test Exercise 1, what is the throw statement?
3. throw waitTime;
Note that the following is an if statement, not a throw statement, even though it
contains a throw statement:
if (waitTime > 30)
throw waitTime;
4. What happens when a throw statement is executed? (Tell what happens in general, not
simply what happens in the code in Self-Test Exercise 1 or some other sample code.)
4. When a throw statement is executed, that is the end of the enclosing try block. No
other statements in the try block are executed, and control passes to the following
catch block or blocks. When we say control passes to the following catch block, we
mean that the value thrown is plugged in for the catch-block parameter (if any) and
the code in the catch block is executed.
5. In the code given in Self-Test Exercise 1, what is the try block?
5. try
{
cout << "Try block entered.";
if (waitTime > 30)
throw waitTime);
cout << "Leaving try block.";
}
6. In the code given in Self-Test Exercise 1, what is the catch block?
6. catch(int thrownValue)
{
cout << "Exception thrown with\n”
<< “waitTime equal to" << thrownValue << endl;
}
7. In the code given in Self-Test Exercise 1, what is the catch-block parameter?
7. thrownValue is the catch-block parameter.
8. What is the output produced by the following program?
#include <iostream>
using std::cout;
void sampleFunction(double test) throw (int);
int main( )
{
try
{
cout << "Trying.\n";
sampleFunction(98.6);
cout << "Trying after call.\n";
}
catch(int)
{
cout << "Catching.\n";
}
cout << "End program.\n";
return 0;
}
void sampleFunction(double test) throw (int)
{
cout << "Starting sampleFunction.\n";
if (test < 100)
throw 42;
}
8. Trying.
Starting sampleFunction.
Catching.
End of program.
9. What is the output produced by the program in Self-Test Exercise 8 when the following
change is made to the program? Change
sampleFunction(98.6);
in the try block to
sampleFunction(212);
9. Trying.
Starting sampleFunction.
Trying after call.
End of program.
10. What happens when an exception is never caught?
10. If an exception is not caught anywhere, then your program ends. Technically speaking,
if an exception is thrown but not caught, then the function terminate( ) is called. The
default meaning for terminate( ) is to end your program.
11. Can you nest a try block inside another try block?
11. Yes, you can have a try block and corresponding catch blocks inside another larger
try block. However, it would probably be better to place the inner try and catch
blocks in a function definition and place an invocation of the function in the larger try
block.
___ ___ allows you to design and code the normal case for your program
separately from the code that handles exceptional situations.
Exception handling
An exception can be thrown in a try block. Alternatively, an exception can be thrown
in a function definition that does not include a try block (or does not include a catch
block to catch that type of exception).
In this case, an invocation of the function can be
placed in a try block.
An ___ is caught in a catch block.
exception
A try block may be followed by more than one catch block.
In this case, always list the
catch block for a more specific exception class before the catch block for a more general
exception class.
The best use of exceptions is ... There is seldom any other situation that can profitably
benefit from throwing an exception.
to throw an exception in a function (but not catch it in
the function) whenever the way the exception is handled will vary from one invocation
of the function to another.
If an exception is thrown in a function but not caught in that function, ...
then the
exception type should be listed in an exception specification for that function.
If an exception is ___ ___ ___ ___, then the default behavior is to end your
program.
thrown but never caught
Do not overuse ___.
exceptions