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.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Introduction to C Programming
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 Improving the User Interface
Introduction to C Programming
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
 2009 Pearson Education, Inc. All rights reserved Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
Java Programming: Guided Learning with Early Objects
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 5 Completing the Inventory Application Introducing Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Screen Scraping Application Introducing String Processing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Exceptions and Assertions Chapter 15 – CSCI 1302.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Lecture 18B Exception Handling and Richard Gesick.
Chapter 11 – Exception Handling
16 Exception Handling.
13 Enhancing the Wage Calculator App
Exception Handling and
Part B – Structured Exception Handling
Fundaments of Game Design
Presentation transcript:

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling

 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

 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

Application Requirements  2009 Pearson Education, Inc. All rights reserved 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%.

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

 2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Enter for the Price and 7.5 for the Annual interest rate (Fig. 25.2). ■Enter non-integer for the Down payment value Figure 25.2 | Entering an invalid value in the Down payment: TextBox.

 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

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

 2009 Pearson Education, Inc. All rights reserved. 9 Test-Driving the Enhanced Car Payment Calculator Application (Cont.) ■Change the Down payment: value to ■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.

 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

 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 Introduction to Exception Handling

 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 Introduction to Exception Handling (Cont.)

 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 Exception Handling in Visual Basic

 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 Exception Handling in Visual Basic (Cont.)

 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 Exception Handling in Visual Basic (Cont.)

 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

 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

 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.)

 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

 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.)

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

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

 2009 Pearson Education, Inc. All rights reserved. 23 ■Run your application and enter invalid input. ■The Exception Assistant (Fig ) 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.)

 2009 Pearson Education, Inc. All rights reserved. 24 Figure | Exception Assistant reveals a FormatException. Type of thrown exception Handling a Format Exception (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 25 ■The code between these two lines (Fig 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 | Enabling exception handling using a Try block. Handling a Format Exception (Cont.) Beginning a Try statement Figure | Ending the Try...Catch block with the End Try keywords. Ending a Try statement

 2009 Pearson Education, Inc. All rights reserved. 26 ■Keyword Catch begins a Catch block (Fig ). ■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 | Handling a FormatException. Handling a Format Exception (Cont.) Catching a FormatException

 2009 Pearson Education, Inc. All rights reserved. 27 ■Lines 54–59 (Fig ) 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 | Displaying a message dialog to the user. Displaying a message when the Catch block executes

 2009 Pearson Education, Inc. All rights reserved. 28 ■Figure presents the source code for the application. Outline (1 of 4 )

 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

 2009 Pearson Education, Inc. All rights reserved. 30 Outline (3 of 4 )

 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

 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 ) Additional Exception Handling Capabilities

 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 Additional Exception Handling Capabilities (Cont.)