Title: Microsoft VB 2005: Reloaded, Advanced
1Microsoft VB 2005 Reloaded, Advanced
- Chapter 5
- Input Validation, Error Handling, and Exception
Handling
2Objectives
- 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
3Objectives (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
4Input 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
5Error Prevention Through Proper Interface Design
6Input 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
7Input Validation by Trapping Keystrokes
(continued)
8Input 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
9Input Validation with the Validating Event
Handler (continued)
10Using 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
11Using the MaskedTextBox Control for Input
Validation (continued)
12Using the MaskedTextBox Control for Input
Validation (continued)
13Using the MaskedTextBox Control for Input
Validation (continued)
14Using the MaskedTextBox Control for Input
Validation (continued)
15Using the MaskedTextBox Control for Input
Validation (continued)
16Using the MaskedTextBox Control for Input
Validation (continued)
17Using 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
18Using the ErrorProvider Component for Input
Validation (continued)
19Using the ErrorProvider Component for Input
Validation (continued)
20Runtime Errors and Exceptions
- Objectives
- Learn about kinds of errors that can occur within
programs - And different ways that you can deal with such
errors
21Types 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
22Types 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
23Types of Errors (continued)
- Structured exception handling
- Process for managing Exception objects that uses
special program elements - Usually preferred over classic error handling
24Types of Errors (continued)
25Runtime 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)
26Runtime Errors in a Simple Application (continued)
27Runtime Errors in a Simple Application (continued)
28Runtime Errors in a Simple Application (continued)
29Runtime Errors in a Simple Application (continued)
- Types of exceptions
- FormatException
- An input string was not in the correct format
- DivideByZeroException
30Runtime Errors in a Simple Application (continued)
31Runtime Errors in a Simple Application (continued)
32Runtime Errors in a Simple Application (continued)
33Runtime Errors in a Simple Application (continued)
34Classic Error Handling in Visual Basic
- Accomplished using the On Error statement
- Of which there are several variations
35On 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
36On Error GoTo ltlinegt (continued)
37Alternatives 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
38On 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
39Disadvantages 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
40Exception 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
41Runtime 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
42Runtime Errors and Exceptions (continued)
43The 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
44The Inheritance Hierarchy of Exception Classes
(continued)
45Structured 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
46The TryCatchFinally Statement
47The TryCatchFinally Statement (continued)
48Definitions 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
49Definitions 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
50How Try, Catch, and Finally Blocks Work
51A Simple TryCatchFinally Example
52Multiple Catch Blocks
53Multiple Catch Blocks (continued)
54More 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
55Programmer-Defined Exceptions
- Programmer-defined exceptions
- Exceptions not associated with standard Visual
Basic runtime errors - Allow your application to handle many additional
unusual situations
56Creating 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
57Creating a Programmer-Defined Exception Class
(continued)
58Throwing Programmer-Defined Exceptions
- Use keyword Throw
- Example
- Throw New IncorrectAgeException(Negative age not
allowed)
59Throwing Programmer-Defined Exceptions (continued)
60Using the InnerException Property
- Display additional information about a particular
exception object
61Using the InnerException Property (continued)
62Summary
- 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
63Summary (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
64Summary (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