Answers to Self Review Exercises


a) throw. b) finally. c) Exception. d) throw point. e) termination, resumption. f) unwind. g) FormatException.


a) False. Exceptions can be handled by other methods on the method-call stack. b) False. User-defined exception classes should extend class ApplicationException. c) True. d) False. A try block that does not have any catch blocks requires a finally block. e) False. The finally block executes only if program control enters the corresponding try block. f) False. return causes control to return to the caller. g) True. h) False. Property Message of class Exception returns a string representing the error message.



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.


Write a program that demonstrates how various exceptions are caught with

catch ( Exception exceptionParameter )

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 derivedclass 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.


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.


Write a program that demonstrates rethrowing an 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 blocksome exceptions can slip through to, and be handled in, other scopes.


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.


Create a GUI application that displays images in a PictureBox. Allow the user to enter the path of the image in a TextBox and click a Button to display the image. If the user enters an invalid file path, a FileNotFoundException will occur. Use exception handling so that a default image will be displayed if an invalid path is entered. Whether a valid path is entered or not, clear the TextBox where the user enters input. Three images have been provided in the examples folder for this chapter in the Ex12_10 directory. Use the image named image0.bmp as the default image. You can use the other two images to test entering a valid path. [Note: You will need to specify that you are using the System.IO namespace for this exercise.]


Create a GUI application 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 strings in the TextBoxes to doubles. If invalid data is entered, a MessageBox should be displayed informing the user.


Create a Vending Machine application (Fig. 12.8) that displays images for four snacks and corresponding Labels that indicate numbers for each snack (the snacks should be numbered 03). Use a string array that contains the names of each snack. The GUI should contain a TextBox in which the user specifies the number of the desired snack. When the Dispense Snack Button is clicked, the name of the selected snack (retrieved from the array) should be displayed. If the user enters a snack value not in the range 03, an IndexOutOfRangeException will occur. Use exception handling so that whenever an IndexOutOfRangeException occurs, a MessageBox is displayed indicating the proper range of values. Also handle any possible FormatExceptions that may occur. The images used in this application can be found in the examples folder for this chapter, in the Ex12_12 directory.

Figure 12.8. Vending Machine application.



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

    Flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net