64
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Embed Size (px)

Citation preview

Page 1: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced

Chapter 5Input Validation, Error Handling, and

Exception Handling

Page 2: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 2

Objectives

• Perform input validation using a variety of techniques

• Describe the differences between runtime errors and exceptions

• Resolve runtime errors in a simple application

• Interpret error messages created by the Common Language Runtime

Page 3: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 3

Objectives (continued)

• Perform classic error handling in an application

• Discuss the inheritance hierarchy of Visual Basic exception classes

• Perform structured error handling in an application

• Create programmer-defined exceptions

Page 4: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 4

Input Validation

• Input validation– Validating user data before it is used by the program

• Techniques– Proper interface design– Trapping keystrokes– The Validating event handler– The MaskedTextBox control– The ErrorProvider component

Page 5: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 5

Error Prevention Through Proper Interface Design

Page 6: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 6

Input Validation by Trapping Keystrokes

• In some cases it is advisable to check each individual keystroke– To determine immediately if the character is valid

• And refrain from displaying it

• .NET Framework KeyPress event– Can be used to check each keystroke made in a

specific control– Properties: Handled and KeyChar

• Char class has several methods to check for keystroke acceptability– IsDigit, IsLetter, and IsWhiteSpace

Page 7: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 7

Input Validation by Trapping Keystrokes (continued)

Page 8: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 8

Input Validation with the Validating Event Handler

• Validate the entire contents of a text box when the Leave event occurs– Event fires when the user Tabs away from the control

or clicks on another control– A Validating event also fires if the CausesValidation property of the control is True

• Validating event enables you to write an event procedure to validate the entire contents of a control

Page 9: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 9

Input Validation with the Validating Event Handler (continued)

Page 10: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 10

Using the MaskedTextBox Control for Input Validation

• MaskedTextBox control– An enhanced TextBox that uses the Mask property

to specify valid user input

• You can create a character mask that specifies:– Required and optional characters– Positions of such characters– Types of required characters – Values and positions of literal characters

Page 11: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 11

Using the MaskedTextBox Control for Input Validation (continued)

Page 12: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 12

Using the MaskedTextBox Control for Input Validation (continued)

Page 13: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 13

Using the MaskedTextBox Control for Input Validation (continued)

Page 14: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 14

Using the MaskedTextBox Control for Input Validation (continued)

Page 15: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 15

Using the MaskedTextBox Control for Input Validation (continued)

Page 16: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 16

Using the MaskedTextBox Control for Input Validation (continued)

Page 17: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 17

Using the ErrorProvider Component for Input Validation

• ErrorProvider component– Notifies the user that a data entry error has occurred

in a particular control• Without using a MessageBox

– Displays a default error icon next to the control where the error occurred

– When the user positions the mouse over the error icon, a ToolTip error message displays

• Informing the user of the problem

Page 18: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 18

Using the ErrorProvider Component for Input Validation

(continued)

Page 19: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 19

Using the ErrorProvider Component for Input Validation

(continued)

Page 20: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 20

Runtime Errors and Exceptions

• Objectives– Learn about kinds of errors that can occur within

programs• And different ways that you can deal with such errors

Page 21: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 21

Types of Errors

• Compile-time errors– Errors that occur while the program is being compiled– Usually due to syntax violation

• Runtime errors– Errors generated by the program during execution

• Logic errors– Errors made by the programmer in designing or

implementing the logic of a program

Page 22: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 22

Types of Errors (continued)

• Classic error handling– Refers to the way Visual Basic internally handles an

error object called Err– Error object is automatically generated by a Visual

Basic program when a runtime error occurs

• Exception– Any exceptional, unusual, or abnormal condition that

occurs during program execution– Exception objects are created by your application

– All Visual Basic runtime errors are exceptions• Not all exceptions are Visual Basic runtime errors

Page 23: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 23

Types of Errors (continued)

• Structured exception handling– Process for managing Exception objects that uses

special program elements– Usually preferred over classic error handling

Page 24: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 24

Types of Errors (continued)

Page 25: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 25

Runtime Errors in a Simple Application

• Division application– Allows the user to divide one number (the dividend)

by another (the divisor), resulting in the answer (the quotient)

Page 26: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 26

Runtime Errors in a Simple Application (continued)

Page 27: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 27

Runtime Errors in a Simple Application (continued)

Page 28: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 28

Runtime Errors in a Simple Application (continued)

Page 29: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 29

Runtime Errors in a Simple Application (continued)

• Types of exceptions– FormatException

• An input string was not in the correct format

– DivideByZeroException

Page 30: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 30

Runtime Errors in a Simple Application (continued)

Page 31: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 31

Runtime Errors in a Simple Application (continued)

Page 32: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 32

Runtime Errors in a Simple Application (continued)

Page 33: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 33

Runtime Errors in a Simple Application (continued)

Page 34: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 34

Classic Error Handling in Visual Basic

• Accomplished using the On Error statement– Of which there are several variations

Page 35: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 35

On Error GoTo <line>

• On Error GoTo <line> statement– Used to place Visual Basic in error-handling mode

• A state of a Visual Basic application that enables the features of classic error handling

– Then jumps to an indicated <line> in the program, which begins the error-handling code

• Known as the error handler

• Will usually display a message to the user and exit the Sub procedure

Page 36: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 36

On Error GoTo <line> (continued)

Page 37: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 37

Alternatives to Exit Sub

• Exit Sub– Exits error-handling mode and immediately leaves the Sub procedure

• Exit Function– Performs error handling within a Function

• Keyword Resume– Repeats execution of the same statement that caused

the error• Resume Next

– Resumes execution with the statement after the one that caused the error

Page 38: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 38

On Error Resume Next

• Allows a program to simply skip a statement that causes an error– And resume normal flow with the next statement

• Errors can then be handled using traditional If...Then logic

Page 39: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 39

Disadvantages of Classic Error Handling

• Classic error handling can be challenging

• It is very difficult to understand how a program handles errors just by looking at the program code

• You cannot nest error-handling code

Page 40: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 40

Exception Classes in the .NET Framework

• Structured exception handling is the preferred way of dealing with runtime errors and other exceptions in a Visual Basic application

Page 41: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 41

Runtime Errors and Exceptions

• A runtime error creates an object called Err• Every runtime error in a Visual Basic program also

generates a specific kind of exception object

• Exception-handling code– Handles special exceptions objects– Can provide important custom error messages to the

user and keep the application running

• Visual Basic’s runtime environment can automatically handle exceptions for you– By displaying standard error messages and

subsequently shutting down the program

Page 42: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 42

Runtime Errors and Exceptions (continued)

Page 43: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 43

The Inheritance Hierarchy of Exception Classes

• Hierarchy begins with the Object class

• Method-call stack– Sequence of method calls within an application that

leads to the creation of an exception object

Page 44: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 44

The Inheritance Hierarchy of Exception Classes (continued)

Page 45: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 45

Structured Exception Handling in Visual Basic .NET

• Objective– Learn how to handle exception objects in a precise

way using structured exception handling• Applications are more robust and fault tolerant

Page 46: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 46

The Try…Catch…Finally Statement

Page 47: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 47

The Try…Catch…Finally Statement (continued)

Page 48: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 48

Definitions of Try, Catch, and Finally Blocks

• Try block– Section of code that could possibly throw (create or

generate) an exception object– Including code that should not execute if an exception is

thrown

• Exception thrower or exception propagator– The method that contains the Try block

• Catch block– Code in a Try...Catch...Finally statement

designed to catch (handle or process) an exception object

Page 49: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 49

Definitions of Try, Catch, and Finally Blocks (continued)

• Exception catcher or exception handler– The method that contains a Catch block

• Finally block– Contains program code that performs final actions after

a Try block or Catch block fully executes– Finally block will always execute whether or not an

exception is thrown– Often used to release any resources created in the Try

block

Page 50: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 50

How Try, Catch, and Finally Blocks Work

Page 51: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 51

A Simple Try…Catch…Finally Example

Page 52: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 52

Multiple Catch Blocks

Page 53: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 53

Multiple Catch Blocks (continued)

Page 54: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 54

More About the Finally Block

• Finally block– It is always executed, regardless of what happens in the Try or Catch blocks

– Block is well suited for closing any application resources that had been previously opened

• Or performing any other kind of program cleanup tasks

Page 55: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 55

Programmer-Defined Exceptions

• Programmer-defined exceptions– Exceptions not associated with standard Visual Basic

runtime errors– Allow your application to handle many additional

unusual situations

Page 56: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 56

Creating a Programmer-Defined Exception Class

• Microsoft recommendations for creating your own exception classes– Give a programmer-defined exception class a

meaningful name that ends with Exception– Class should derive from ApplicationException

• To provide the application with the required exception-handling functionality

Page 57: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 57

Creating a Programmer-Defined Exception Class (continued)

Page 58: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 58

Throwing Programmer-Defined Exceptions

• Use keyword Throw• Example

– Throw New IncorrectAgeException(“Negative age not allowed”)

Page 59: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 59

Throwing Programmer-Defined Exceptions (continued)

Page 60: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 60

Using the InnerException Property

• Display additional information about a particular exception object

Page 61: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 61

Using the InnerException Property (continued)

Page 62: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 62

Summary

• Minimize errors through the proper design of the user interface– .NET Framework KeyPress event– MaskedTextBox control– ErrorProvider component

• Types of errors– Compile-time errors– Runtime errors– Logic errors

• In Visual Basic, a runtime error creates an object called Err, which has certain properties

Page 63: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 63

Summary (continued)

• An exception is any kind of unusual or infrequent problem that occurs during program execution, including runtime errors

• Structured exception handling manages program problems using exception objects

• Classic error handling is the Visual Basic process for managing error objects generated by the program when standard runtime errors occur

• Visual Basic structured exception handling uses the Try...Catch...Finally statement

Page 64: Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling

Microsoft VB 2005: Reloaded, Advanced 64

Summary (continued)

• The inheritance hierarchy for .NET Framework exception classes begins with the Object class– And continues at the next lower level with the Exception class

• You can define your own custom exception classes, called programmer-defined exceptions