Sams Teach Yourself C++ in 21 Days

(singke) #1

  1. Should you pass exceptions by value or by reference?

  2. Will a catchstatement catch a derived exception if it is looking for the base class?

  3. If two catchstatements are used, one for base and one for derived, which should
    come first?

  4. What does catch(...)mean?

  5. What is a breakpoint?


Exercises ........................................................................................................



  1. Create a tryblock, a catchstatement, and a simple exception.

  2. Modify the answer from Exercise 1, put data into the exception along with an
    accessor function, and use it in the catchblock.

  3. Modify the class from Exercise 2 to be a hierarchy of exceptions. Modify the
    catchblock to use the derived objects and the base objects.

  4. Modify the program from Exercise 3 to have three levels of function calls.
    5.BUG BUSTERS:What is wrong with the following code?
    #include “stringc.h” // our string class


class xOutOfMemory
{
public:
xOutOfMemory( const String& where ) : location( where ){}
~xOutOfMemory(){}
virtual String where(){ return location };
private:
String location;
}

int main()
{
try
{
char *var = new char;
if ( var == 0 )
throw xOutOfMemory();
}
catch( xOutOfMemory& theException )
{
cout << “Out of memory at “ << theException.location() << endl;
}
return 0;
}
This listing shows exception handling for handling an out-of-memory error.

750 Day 20

Free download pdf