12.3 |
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.
|
12.4 |
Write a program that demonstrates how various exceptions are caught with
catch ( Exception exceptionParameter )
|
12.5 |
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.
|
12.6 |
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.
|
12.7 |
Write a program that demonstrates rethrowing an exception.
|
|
|
12.8 |
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.
|
12.9 |
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.
|
12.10 |
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.]
|
12.11 |
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.
|
12.12 |
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.
|