Microsoft VB 2005: Reloaded, Advanced - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Microsoft VB 2005: Reloaded, Advanced

Description:

Validating user data before it is used by the program ... Can be used to check each keystroke made in a specific control. Properties: Handled and KeyChar ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 65
Provided by: cecsC
Category:

less

Transcript and Presenter's Notes

Title: Microsoft VB 2005: Reloaded, Advanced


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

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

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

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

5
Error Prevention Through Proper Interface Design
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

7
Input Validation by Trapping Keystrokes
(continued)
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

9
Input Validation with the Validating Event
Handler (continued)
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

11
Using the MaskedTextBox Control for Input
Validation (continued)
12
Using the MaskedTextBox Control for Input
Validation (continued)
13
Using the MaskedTextBox Control for Input
Validation (continued)
14
Using the MaskedTextBox Control for Input
Validation (continued)
15
Using the MaskedTextBox Control for Input
Validation (continued)
16
Using the MaskedTextBox Control for Input
Validation (continued)
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

18
Using the ErrorProvider Component for Input
Validation (continued)
19
Using the ErrorProvider Component for Input
Validation (continued)
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

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

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

23
Types of Errors (continued)
  • Structured exception handling
  • Process for managing Exception objects that uses
    special program elements
  • Usually preferred over classic error handling

24
Types of Errors (continued)
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)

26
Runtime Errors in a Simple Application (continued)
27
Runtime Errors in a Simple Application (continued)
28
Runtime Errors in a Simple Application (continued)
29
Runtime Errors in a Simple Application (continued)
  • Types of exceptions
  • FormatException
  • An input string was not in the correct format
  • DivideByZeroException

30
Runtime Errors in a Simple Application (continued)
31
Runtime Errors in a Simple Application (continued)
32
Runtime Errors in a Simple Application (continued)
33
Runtime Errors in a Simple Application (continued)
34
Classic Error Handling in Visual Basic
  • Accomplished using the On Error statement
  • Of which there are several variations

35
On Error GoTo ltlinegt
  • On Error GoTo ltlinegt 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 ltlinegt 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

36
On Error GoTo ltlinegt (continued)
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

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

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

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

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 Basics runtime environment can
    automatically handle exceptions for you
  • By displaying standard error messages and
    subsequently shutting down the program

42
Runtime Errors and Exceptions (continued)
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

44
The Inheritance Hierarchy of Exception Classes
(continued)
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

46
The TryCatchFinally Statement
47
The TryCatchFinally Statement (continued)
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

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

50
How Try, Catch, and Finally Blocks Work
51
A Simple TryCatchFinally Example
52
Multiple Catch Blocks
53
Multiple Catch Blocks (continued)
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

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

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

57
Creating a Programmer-Defined Exception Class
(continued)
58
Throwing Programmer-Defined Exceptions
  • Use keyword Throw
  • Example
  • Throw New IncorrectAgeException(Negative age not
    allowed)

59
Throwing Programmer-Defined Exceptions (continued)
60
Using the InnerException Property
  • Display additional information about a particular
    exception object

61
Using the InnerException Property (continued)
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

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

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
Write a Comment
User Comments (0)
About PowerShow.com