Exercises

  1. 17.15 (Exceptional Conditions) List various exceptional conditions that have occurred throughout this text. List as many additional exceptional conditions as you can. For each of these exceptions, describe briefly how a program typically would handle the exception, using the exception-handling techniques discussed in this chapter. Some typical exceptions are division by zero, arithmetic overflow, array subscript out of bounds, exhaustion of the free store, etc.

  2. 17.16 (Catch Parameter) Under what circumstances would you not provide a parameter name when defining the type of the object that will be caught by a handler?

  3. 17.17 (throw Statement) A program contains the statement

    
    throw;
    

    Where would you normally expect to find such a statement? What if that statement appeared in a different part of the program?

  4. 17.18 (Exception Handling vs. Other Schemes) Compare and contrast exception handling with the various other error-processing schemes discussed in the text.

  5. 17.19 (Exception Handling and Program Control) Why should exceptions not be used as an alternate form of program control?

  6. 17.20 (Handling Related Exceptions) Describe a technique for handling related exceptions.

  7. 17.21 (Throwing Exceptions from a catch) Suppose a program throws an exception and the appropriate exception handler begins executing. Now suppose that the exception handler itself throws the same exception. Does this create infinite recursion? Write a program to check your observation.

  8. 17.22 (Catching Derived-Class Exceptions) Use inheritance to create various derived classes of runtime_error. Then show that a catch handler specifying the base class can catch derived-class exceptions.

  9. 17.23 (Throwing the Result of a Conditional Expression) Throw the result of a conditional expression that returns either a double or an int. Provide an int catch handler and a double catch handler. Show that only the double catch handler executes, regardless of whether the int or the double is returned.

  10. 17.24 (Local-Variable Destructors) Write a program illustrating that all destructors for objects constructed in a block are called before an exception is thrown from that block.

  11. 17.25 (Member-Object Destructors) Write a program illustrating that member-object destructors are called for only those member objects that were constructed before an exception occurred.

  12. 17.26 (Catching All Exceptions) Write a program that demonstrates several exception types being caught with the catch(...) exception handler.

  13. 17.27 (Order of Exception Handlers) Write a program illustrating that the order of exception handlers is important. The first matching handler is the one that executes. Attempt to compile and run your program two different ways to show that two different handlers execute with two different effects.

  14. 17.28 (Constructors Throwing Exceptions) Write a program that shows a constructor passing information about constructor failure to an exception handler after a try block.

  15. 17.29 (Rethrowing Exceptions) Write a program that illustrates rethrowing an exception.

  16. 17.30 (Uncaught Exceptions) Write a program that illustrates that a function with its own try block does not have to catch every possible error generated within the try. Some exceptions can slip through to, and be handled in, outer scopes.

  17. 17.31 (Stack Unwinding) Write a program that throws an exception from a deeply nested function and still has the catch handler following the try block enclosing the initial call in main catch the exception.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset