Download presentation
Presentation is loading. Please wait.
Published byMarvin Holmes Modified over 8 years ago
1
T U T O R I A L 2009 Pearson Education, Inc. All rights reserved. 1 25 Enhanced Car Payment Calculator Application Introducing Exception Handling
2
2009 Pearson Education, Inc. All rights reserved. 2 Outline 25.1 Test-Driving the Enhanced Car Payment Calculator Application: 25.2 Introduction to Exception Handling 25.3 Exception Handling in Visual Basic 25.4 Constructing the Enhanced Car Payment Calculator Application 25.5 Additional Exception Handling Capabilities
3
2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Use exception handling. ■Use the Try, Catch and Finally blocks to handle exceptions. ■Use the Throw statement to indicate an exception and to specify that an existing exception needs further processing. Objectives
4
Application Requirements 2009 Pearson Education, Inc. All rights reserved. 4 25.1 Test-Driving the Enhanced Car Payment Calculator Application: A bank wishes to accept only valid data from users on their car loans. Although the existing application calculates a result when incorrect data is entered, this result does not correctly represent the user’s input. If the user enters anything besides an Integer for the price or down payment, or a Double for the interest rate, a message dialog should be displayed instructing the user to input proper data. T he interest rate should be entered such that an input of 5 is equal to 5%.
5
2009 Pearson Education, Inc. All rights reserved. 5 Test-Driving the Enhanced Car Payment Calculator Application ■Run the completed application (Fig. 25.1). Figure 25.1 | Running the completed Enhanced Car Payment Calculator application.
6
2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Enter 16900 for the Price and 7.5 for the Annual interest rate (Fig. 25.2). ■Enter non-integer 6000.50 for the Down payment value Figure 25.2 | Entering an invalid value in the Down payment: TextBox.
7
2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Click the Calculate Button. ■An error message (Fig. 25.3) appears. Figure 25.3 | Entering an invalid value in the Down payment: TextBox. Displaying a message when an exception is thrown
8
2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Change the Down payment: value to the non- numeric 600p (Fig. 25.4). ■Click the Calculate Button, and the message dialog appears again. Figure 25.4 | Entering non-numeric data in the Down Payment: TextBox.
9
2009 Pearson Education, Inc. All rights reserved. 9 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Change the Down payment: value to 6000. ■Enter 7.5% for the Annual interest rate : value (Fig. 25.5). ■When you click the Calculate Button, the message dialog appears again. Figure 25.5 | Entering non-numeric data in the Annual interest rate : TextBox.
10
2009 Pearson Education, Inc. All rights reserved. 10 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Change the Annual interest rate value to 7.5. ■Click the Calculate Button (Fig. 25.6). Figure 25.6 | Displaying monthly payments after input is corrected. Results displayed only when valid input is entered
11
2009 Pearson Education, Inc. All rights reserved. 11 Perform a task If the preceding task did not execute correctly Perform error processing Perform the next task If the preceding task did not execute correctly Perform error processing... 25.2 Introduction to Exception Handling
12
2009 Pearson Education, Inc. All rights reserved. 12 ■Exception handling enables you to remove error- handling code from your application’s logic. ■A method throws an exception if a problem occurs during the method execution. –If there is an exception handler, it catches and handles the exception. –If there is not, the exception is sent to the calling method, which may or may not handle it. ■An uncaught exception likely causes application execution to terminate. 25.2 Introduction to Exception Handling (Cont.)
13
2009 Pearson Education, Inc. All rights reserved. 13 ■A Try statement consists of: –a Try block –at least one Catch block –the terminating keywords End Try. ■The Try block encloses statements that might cause exceptions. 25.3 Exception Handling in Visual Basic
14
2009 Pearson Education, Inc. All rights reserved. 14 ■An exception is thrown using the Throw keyword followed by the exception object: Throw New Exception() ■A Catch block contains code that handles an exception and allows the application to continue executing correctly. ■A Catch block can specify a parameter that identifies the type of exception the exception handler can process. ■A Catch block that does not specify a parameter catches all exceptions. –This should be placed after all other Catch blocks. 25.3 Exception Handling in Visual Basic (Cont.)
15
2009 Pearson Education, Inc. All rights reserved. 15 ■If an exception occurs, the Try block terminates immediately. ■Next, the application searches for the first Catch block that can process the exception type. ■When a match occurs, the code in Catch block executes. ■If no exceptions occur in a Try block, the application ignores the Catch block. 25.3 Exception Handling in Visual Basic (Cont.)
16
2009 Pearson Education, Inc. All rights reserved. 16 When the user clicks the Calculate Button: Clear the ListBox of any previous text Try Get the car price from the Price: TextBox Get the down payment from the Down payment: TextBox Get the annual interest rate from the Annual interest rate: TextBox Calculate the loan amount (price minus down payment) Calculate the monthly interest rate (annual interest rate divided by 12) Calculate and display the monthly payments for 2, 3, 4 and 5 years Catch Display the error message dialog 25.4 Constructing the Enhanced Car Payment Calculator Application
17
2009 Pearson Education, Inc. All rights reserved. 17 Figure 25.7 | Enhanced Car Payment Calculator application ACE table. (Part 1 of 2.) ■Use an ACE table to convert the pseudocode into Visual Basic (Fig. 25.7). Action/Control/Event (ACE) Table for the Enhanced Car Payment Calculator Application
18
2009 Pearson Education, Inc. All rights reserved. 18 Action/Control/Event (ACE) Table for the Enhanced Car Payment Calculator Application (Cont.) Figure 25.7 | Enhanced Car Payment Calculator application ACE table. (Part 2 of 2.)
19
2009 Pearson Education, Inc. All rights reserved. 19 ■Open the template application (Fig. 25.8). ■These three statements must explicitly convert the data in the TextBox es to Integer and Double values. Figure 25.8 | Val ensures data is in numeric format. Handling a Format Exception
20
2009 Pearson Education, Inc. All rights reserved. 20 ■Method Convert.ToInt32 throws a FormatException if it cannot convert its argument to an Integer. –The FormatException class occurs when a method is passed an argument that is not in the expected format. Handling a Format Exception (Cont.)
21
2009 Pearson Education, Inc. All rights reserved. 21 Error-Prevention Tip Before using a method, read its online documentation to determine whether it throws exceptions. If so, use the exception-handling techniques of this chapter to help make your code more robust. To access the online documentation for a method in the.NET Framework Class Library, you can click its name in the source-code editor and press F1.
22
2009 Pearson Education, Inc. All rights reserved. 22 ■Remove the Val function call and the parentheses that designate its argument (Fig. 25.9). –Removing the Val function call causes Convert.ToInt32 and Convert.ToDouble to throw an exception if invalid input is entered into one of the TextBox es. Handling a Format Exception (Cont.) Removing the Val function call allows exceptions to be thrown Figure 25.9 | Removing the Val function call from the application.
23
2009 Pearson Education, Inc. All rights reserved. 23 ■Run your application and enter invalid input. ■The Exception Assistant (Fig. 25.10) appears. –The exception assistant indicates where the exception occurred and the type of the exception. –It also provides links to helpful information on handling the exception. Handling a Format Exception (Cont.)
24
2009 Pearson Education, Inc. All rights reserved. 24 Figure 25.10 | Exception Assistant reveals a FormatException. Type of thrown exception Handling a Format Exception (Cont.)
25
2009 Pearson Education, Inc. All rights reserved. 25 ■The code between these two lines (Fig. 25.11 and 25.12) is the code that might throw an exception and code that you do not want to execute if an exception occurs. Figure 25.11 | Enabling exception handling using a Try block. Handling a Format Exception (Cont.) Beginning a Try statement Figure 25.12 | Ending the Try...Catch block with the End Try keywords. Ending a Try statement
26
2009 Pearson Education, Inc. All rights reserved. 26 ■Keyword Catch begins a Catch block (Fig. 25.13). ■A Catch block ends when either another Catch block, a Finally block or the End Try keywords are reached. ■This Catch block executes if a FormatException occurs. Figure 25.13 | Handling a FormatException. Handling a Format Exception (Cont.) Catching a FormatException
27
2009 Pearson Education, Inc. All rights reserved. 27 ■Lines 54–59 (Fig. 25.14) display a MessageBox instructing the user to enter valid input. ■Note that the MessageBoxIcon.Error icon is used. ■Run the application, and test the application with valid and invalid inputs. Handling a Format Exception (Cont.) Figure 25.14 | Displaying a message dialog to the user. Displaying a message when the Catch block executes
28
2009 Pearson Education, Inc. All rights reserved. 28 ■Figure 25.15 presents the source code for the application. Outline (1 of 4 )
29
2009 Pearson Education, Inc. All rights reserved. 29 Outline (2 of 4 ) Beginning the Try block Removing the Val method calls allows exceptions to be thrown
30
2009 Pearson Education, Inc. All rights reserved. 30 Outline (3 of 4 )
31
2009 Pearson Education, Inc. All rights reserved. 31 Outline (4 of 4 ) Catching a FormatException Displaying a MessageBox when a FormatException occurs Ending the Try statement
32
2009 Pearson Education, Inc. All rights reserved. 32 ■A Finally block can be placed before End Try –This specifies code that always executes, whether or not an exception occurs. –This is particularly useful when working with resources that require explicit action to release them (e.g. StreamReader or StreamWriter ). 25.5 Additional Exception Handling Capabilities
33
2009 Pearson Education, Inc. All rights reserved. 33 ■A Catch block that cannot process a certain exception or can only partially process the exception –In this case, rethrow the exception using the following Throw statement, where exceptionReference is the parameter for the exception. Throw exceptionReference –The next enclosing Try statement attempts to catch the exception. 25.5 Additional Exception Handling Capabilities (Cont.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.