Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.

Similar presentations


Presentation on theme: "IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation."— Presentation transcript:

1 IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Topics Validation and Error Handling Validation Exception Handling Complex Exception Handling Throwing Your Own Exceptions (errors) “Foolproof systems don’t take into account the ingenuity of fools” Gene Brown

2 IMS 3253: Validation and Errors 2 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Errors & Validation Computer systems consist of a dizzying collection of rules –Laws of physics The database server must be plugged in to work –Rules of the programming language Convert.ToSingle(“Green”) will not work –Rules of the organization Prices must be positive numbers

3 IMS 3253: Validation and Errors 3 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Errors & Validation Some problems can not be prevented by the programmer –The database connection is unavailable –The user is an idiot Some problems can be anticipated –The database connection is unavailable –The user is an idiot Some problems can be anticipated and prevented –The user left the quantity text box blank –The user entered a “$” in the price text box

4 IMS 3253: Validation and Errors 4 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Errors & Validation Proper program design attempts to keep the program from crashing when an error is encountered –Let the program take action –Let the user take action –Provide a more graceful exit if the program is not fixable –Provide feedback to programmers and operators

5 IMS 3253: Validation and Errors 5 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Errors & Validation (cont.) Adopt a multi-layered approach to problems –Validate data before working with it Application level (what we are doing now) Database level (ISM 4212) –Handle errors using Try Catch blocks Anticipate likely problems Notify users of fixable conditions Log errors and notify system operators You can easily write as much code to validate and handle problems as for write business logic

6 IMS 3253: Validation and Errors 6 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data There are a wide variety of strategies and techniques for validating user input Often multiple validations are required for a single control Each control may have a different technique for validating input See sample application for illustrations

7 IMS 3253: Validation and Errors 7 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data—Text Boxes Text boxes may require multiple tests for the same text box –Testing to ensure that a value is present Don’t forget to test for just spaces If Trim(txtBox.Text).Length = 0 Then –Value can be converted to another data type If Not IsNumeric(txtBox.Text) Then If Not IsDate(txtBox.Text) Then –Value passes business rules If Convert.ToSingle(txtPrice.Text) <= 0 Then

8 IMS 3253: Validation and Errors 8 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data—Strategies You can validate individual control data when inputs are made –See looping validation example to guarantee valid characters in a text box You can validate in the LostFocus event –Avoid refocusing or message boxes in this event Avoid strategies that notify user of one problem at a time Avoid excessive use of Message Boxes

9 IMS 3253: Validation and Errors 9 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data—Strategies (cont.) I prefer to perform all validations at one time prior to processing the data Use a Boolean blnIsValid = True variable to start Each test sets blnIsValid = False if the test fails Test blnIsValid before processing –If False—notify user, do not process –If True—continue processing Use strategies to notify user of all error conditions Combine with individual problem notification See sample application

10 IMS 3253: Validation and Errors 10 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data—Strategies (cont.) Dim blnIsValid as Boolean = True Dim strError as String = “These problems occurred:” _ & vbCrLf If Then blnIsValid = False strError &= vbCrLf & “Message about this error” End If * Repeat logic above for each test If blnIsValid Then Else Notify user End If *

11 IMS 3253: Validation and Errors 11 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Validating Data—Strategies (cont.) Next week we will see how to move validation logic out of the main event code and into a function Be sure to reset any interface notifications to the user when reattempting code that did not pass validation before

12 IMS 3253: Validation and Errors 12 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Error Handling Programming languages usually provide a mechanism to trap and handle errors VB.Net does this with a Try…Catch block Try Catch ex As Exception Finally ‘* Optional End Try

13 IMS 3253: Validation and Errors 13 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Error Handling (cont.) Try and End Try delimit the scope of the error handler If an error occurs between the Try statement and the Catch statements –Processing of the error-causing code stops –Execution is transferred to the first line inside the Catch block The rest of the ‘main’ code is skipped –The error message is not shown to the user –Processing continues

14 IMS 3253: Validation and Errors 14 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Error Handling (cont.) If present, a Finally block’s code will always execute –Whether an error occurred or not May be used to close open data files or database connections

15 IMS 3253: Validation and Errors 15 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Multiple Catch Blocks Catch ex As Exception allows for one generic error handler You can test for what kind of error was thrown to control action Select Case ex.GetType.ToString Case “System.FormatException” Case “System.OverFlowException” : End Select

16 IMS 3253: Validation and Errors 16 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Multiple Catch Blocks (cont.) You can also have multiple Catch blocks with each catching a different kind of error Catch ex As OverflowException Catch ex As IndexOutOfRangeException : Catch ex As Exception ‘* Generic handler will catch any other kind ‘* of error End Try

17 IMS 3253: Validation and Errors 17 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Throwing Your Own Exceptions You can write code to throw an exception for two reasons –Testing your exception handler –When writing classes that will be consumed by other programs Classes are blocks of code that can be added to other programs but can’t be run on their own Exceptions thrown by your class will percolate up to the program using the class

18 IMS 3253: Validation and Errors 18 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Throwing Your Own Exceptions (cont.) Syntax to throw an exception with a specialized message Throw New Exception(“MessageToSend”) The Exception can be the generic Exception type or a specific Exception type –OverflowException –FormatException –IndexOutOfRangeException


Download ppt "IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation."

Similar presentations


Ads by Google