Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling."— Presentation transcript:

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

2 Microsoft VB 2005: Reloaded, Advanced2 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 Microsoft VB 2005: Reloaded, Advanced3 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 Microsoft VB 2005: Reloaded, Advanced4 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 Microsoft VB 2005: Reloaded, Advanced5 Error Prevention Through Proper Interface Design

6 Microsoft VB 2005: Reloaded, Advanced6 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 Microsoft VB 2005: Reloaded, Advanced7 Input Validation by Trapping Keystrokes (continued)

8 Microsoft VB 2005: Reloaded, Advanced8 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 Microsoft VB 2005: Reloaded, Advanced9 Input Validation with the Validating Event Handler (continued)

10 Microsoft VB 2005: Reloaded, Advanced10 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 Microsoft VB 2005: Reloaded, Advanced11 Using the MaskedTextBox Control for Input Validation (continued)

12 Microsoft VB 2005: Reloaded, Advanced12 Using the MaskedTextBox Control for Input Validation (continued)

13 Microsoft VB 2005: Reloaded, Advanced13 Using the MaskedTextBox Control for Input Validation (continued)

14 Microsoft VB 2005: Reloaded, Advanced14 Using the MaskedTextBox Control for Input Validation (continued)

15 Microsoft VB 2005: Reloaded, Advanced15 Using the MaskedTextBox Control for Input Validation (continued)

16 Microsoft VB 2005: Reloaded, Advanced16 Using the MaskedTextBox Control for Input Validation (continued)

17 Microsoft VB 2005: Reloaded, Advanced17 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 Microsoft VB 2005: Reloaded, Advanced18 Using the ErrorProvider Component for Input Validation (continued)

19 Microsoft VB 2005: Reloaded, Advanced19 Using the ErrorProvider Component for Input Validation (continued)

20 Microsoft VB 2005: Reloaded, Advanced20 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 Microsoft VB 2005: Reloaded, Advanced21 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 Microsoft VB 2005: Reloaded, Advanced22 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 Microsoft VB 2005: Reloaded, Advanced23 Types of Errors (continued) Structured exception handling –Process for managing Exception objects that uses special program elements –Usually preferred over classic error handling

24 Microsoft VB 2005: Reloaded, Advanced24 Types of Errors (continued)

25 Microsoft VB 2005: Reloaded, Advanced25 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 Microsoft VB 2005: Reloaded, Advanced26 Runtime Errors in a Simple Application (continued)

27 Microsoft VB 2005: Reloaded, Advanced27 Runtime Errors in a Simple Application (continued)

28 Microsoft VB 2005: Reloaded, Advanced28 Runtime Errors in a Simple Application (continued)

29 Microsoft VB 2005: Reloaded, Advanced29 Runtime Errors in a Simple Application (continued) Types of exceptions –FormatException An input string was not in the correct format –DivideByZeroException

30 Microsoft VB 2005: Reloaded, Advanced30 Runtime Errors in a Simple Application (continued)

31 Microsoft VB 2005: Reloaded, Advanced31 Runtime Errors in a Simple Application (continued)

32 Microsoft VB 2005: Reloaded, Advanced32 Runtime Errors in a Simple Application (continued)

33 Microsoft VB 2005: Reloaded, Advanced33 Runtime Errors in a Simple Application (continued)

34 Microsoft VB 2005: Reloaded, Advanced34 Classic Error Handling in Visual Basic Accomplished using the On Error statement –Of which there are several variations

35 Microsoft VB 2005: Reloaded, Advanced35 On Error GoTo On Error GoTo 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 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 Microsoft VB 2005: Reloaded, Advanced36 On Error GoTo (continued)

37 Microsoft VB 2005: Reloaded, Advanced37 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 Microsoft VB 2005: Reloaded, Advanced38 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 Microsoft VB 2005: Reloaded, Advanced39 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 Microsoft VB 2005: Reloaded, Advanced40 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 Microsoft VB 2005: Reloaded, Advanced41 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

42 Microsoft VB 2005: Reloaded, Advanced42 Runtime Errors and Exceptions (continued)

43 Microsoft VB 2005: Reloaded, Advanced43 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 Microsoft VB 2005: Reloaded, Advanced44 The Inheritance Hierarchy of Exception Classes (continued)

45 Microsoft VB 2005: Reloaded, Advanced45 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 Microsoft VB 2005: Reloaded, Advanced46 The Try…Catch…Finally Statement

47 Microsoft VB 2005: Reloaded, Advanced47 The Try…Catch…Finally Statement (continued)

48 Microsoft VB 2005: Reloaded, Advanced48 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 Microsoft VB 2005: Reloaded, Advanced49 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 Microsoft VB 2005: Reloaded, Advanced50 How Try, Catch, and Finally Blocks Work

51 Microsoft VB 2005: Reloaded, Advanced51 A Simple Try…Catch…Finally Example

52 Microsoft VB 2005: Reloaded, Advanced52 Multiple Catch Blocks

53 Microsoft VB 2005: Reloaded, Advanced53 Multiple Catch Blocks (continued)

54 Microsoft VB 2005: Reloaded, Advanced54 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 Microsoft VB 2005: Reloaded, Advanced55 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 Microsoft VB 2005: Reloaded, Advanced56 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 Microsoft VB 2005: Reloaded, Advanced57 Creating a Programmer-Defined Exception Class (continued)

58 Microsoft VB 2005: Reloaded, Advanced58 Throwing Programmer-Defined Exceptions Use keyword Throw Example –Throw New IncorrectAgeException(“Negative age not allowed”)

59 Microsoft VB 2005: Reloaded, Advanced59 Throwing Programmer-Defined Exceptions (continued)

60 Microsoft VB 2005: Reloaded, Advanced60 Using the InnerException Property Display additional information about a particular exception object

61 Microsoft VB 2005: Reloaded, Advanced61 Using the InnerException Property (continued)

62 Microsoft VB 2005: Reloaded, Advanced62 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 Microsoft VB 2005: Reloaded, Advanced63 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 Microsoft VB 2005: Reloaded, Advanced64 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


Download ppt "Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling."

Similar presentations


Ads by Google