html5-img
1 / 63

Microsoft VB 2005: Reloaded, Advanced

Microsoft VB 2005: Reloaded, Advanced. 2. Objectives. Perform input validation using a variety of techniquesDescribe the differences between runtime errors and exceptionsResolve runtime errors in a simple applicationInterpret error messages created by the Common Language Runtime. Microsoft VB 200

lola
Download Presentation

Microsoft VB 2005: Reloaded, Advanced

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

    5. Microsoft VB 2005: Reloaded, Advanced 5 Error Prevention Through Proper Interface Design

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

    7. Microsoft VB 2005: Reloaded, Advanced 7 Input Validation by Trapping Keystrokes (continued)

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

    9. Microsoft VB 2005: Reloaded, Advanced 9 Input Validation with the Validating Event Handler (continued)

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

    11. Microsoft VB 2005: Reloaded, Advanced 11 Using the MaskedTextBox Control for Input Validation (continued)

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

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

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

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

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

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

    18. Microsoft VB 2005: Reloaded, Advanced 18 Using the ErrorProvider Component for Input Validation (continued)

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

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

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

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

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

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

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

    26. Microsoft VB 2005: Reloaded, Advanced 26 Runtime Errors in a Simple Application (continued)

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

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

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

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

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

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

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

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

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

    36. Microsoft VB 2005: Reloaded, Advanced 36 On Error GoTo <line> (continued)

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

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

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

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

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

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

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

    44. Microsoft VB 2005: Reloaded, Advanced 44 The Inheritance Hierarchy of Exception Classes (continued)

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

    46. Microsoft VB 2005: Reloaded, Advanced 46 The TryCatchFinally Statement

    47. Microsoft VB 2005: Reloaded, Advanced 47 The TryCatchFinally Statement (continued)

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

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

    50. Microsoft VB 2005: Reloaded, Advanced 50 How Try, Catch, and Finally Blocks Work

    51. Microsoft VB 2005: Reloaded, Advanced 51 A Simple TryCatchFinally Example

    52. Microsoft VB 2005: Reloaded, Advanced 52 Multiple Catch Blocks

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

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

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

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

    57. Microsoft VB 2005: Reloaded, Advanced 57 Creating a Programmer-Defined Exception Class (continued)

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

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

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

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

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

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

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

More Related