Exercises

  1. 13.3 (Exception Base Classes and Derived Classes) Use inheritance to create an exception base class and various exception-derived classes. Write a program to demonstrate that the catch specifying the base class catches derived-class exceptions.

  2. 13.4 (Catching Exceptions) Write a program that demonstrates how various exceptions are caught with

    
    catch (Exception ex)
    
  3. 13.5 (Order of Exception Handlers) To demonstrate the importance of the order of exception handlers, write two programs, one with correct ordering of catch blocks (i.e., place the base-class exception handler after all derived-class exception handlers) and another with improper ordering (i.e., place the base-class exception handler before the derived-class exception handlers). What happens when you attempt to compile the second program?

  4. 13.6 (Constructor Failure) Exceptions can be used to indicate problems that occur when an object is being constructed. Write a program that shows a constructor passing information about constructor failure to an exception handler. The exception thrown also should contain the arguments sent to the constructor.

  5. 13.7 (Rethrowing and Exception) Write a program that demonstrates rethrowing an exception.

  6. 13.8 (Not Catching Every Exception) Write a program demonstrating that a method with its own try block does not have to catch every possible exception that occurs within the try block—some exceptions can slip through to, and be handled in, other scopes.

  7. 13.9 (Exception from a Deeply Nested Method) Write a program that throws an exception from a deeply nested method. The catch block should follow the try block that encloses the call chain. The exception caught should be one you defined yourself. In catching the exception, display the exception’s message and stack trace.

  8. 13.10 (FormatExceptions) Create an app that inputs miles driven and gallons used, and calculates miles per gallon. The example should use exception handling to process the FormatExceptions that occur when converting the input strings to doubles. If invalid data is entered, display a message informing the user.

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

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