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

Slides:



Advertisements
Similar presentations
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
Object Oriented Software Development 8. Exceptions, testing and debugging.
Introduction to Exception Handling and Defensive Programming.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
IMS 3253: Controls 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Check Boxes Radio Buttons Date Time Picker Masked.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 3253: Forms, Controls, Properties, Events 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Program Elements Assignment.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Date and Time Data Turning Strings into.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Exception testing Aistis Karkauskas, IFM-2/2. Introduction  Exceptions – anomalous or exceptional events requiring special processing – often changing.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
E-Commerce: Introduction to ASP.Net Application Architecture 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics.
A variable is a name for a value stored in memory.
Computer Programming I
Chapter 14: Exception Handling
Topics Introduction to File Input and Output
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Part B – Structured Exception Handling
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Topics Introduction to File Input and Output
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 2 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 3 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 4 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 5 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 6 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 7 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 8 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 9 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 10 Dr. Lawrence West, MIS Dept., University of Central Florida 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 *

IMS 3253: Validation and Errors 11 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 12 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 13 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 14 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 15 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 16 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 17 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 3253: Validation and Errors 18 Dr. Lawrence West, MIS Dept., University of Central Florida 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