Download presentation
Presentation is loading. Please wait.
1
Part B – Structured Exception Handling
Lecture Set 8 Validating Data and Handling Exceptions Part B – Structured Exception Handling
2
Objectives To understand how to us the Structured Exception handling tools of Visual Basic Describe the Exception hierarchy and name two of its subclasses. Describe the use of Try…Catch statements to catch specific exceptions as well as all exceptions. Learn how to throw exceptions and how to catch them and take appropriate steps to prevent software failure 12/8/2018 7:20 AM
3
Objectives (continued)
Describe the use of the properties and methods of an exception object. Describe the use of Throw statements. Describe the three types of data validation that you’re most likely to perform on a user entry. Describe two ways that you can use generic validation methods in a method that validates all of the user entries for a form. Describe the use the Validating event and the use of masked text boxes for data validation. 12/8/2018 7:20 AM
4
Structured Exception Handling
Run-time errors will cause a program to terminate because of an exception being thrown Exceptions can be thrown for several reasons Numeric overflow errors Type conversion errors Division by zero errors Database errors of all sorts Null reference exceptions Create structured exception handlers to prevent a program from terminating 12/8/2018 7:20 AM
5
The Exception Class Hierarchy
12/8/2018 7:20 AM
7
Why are Unhandled Exceptions Bad?
In a nutshell – software that fails for any reason is unacceptable Software has to be written to accept any input from the user (or from external events) and react appropriately 12/8/2018 7:20 AM
8
12/8/2018 7:20 AM
9
The System.Exception Class
All exceptions are derived from the System.Exception class Properties The Message property contains an informational message The Source property is a string containing the name of the application causing the error The StackTrace property returns a string containing the error location 12/8/2018 7:20 AM
10
Do We Need Structured Exception Handling?
Can’t we program without this? Can’t we program without structured alternatives and repetitions Can’t we program without classes? Can’t we program without validation structures and tools? Can’t I write good code without having to worry about the accessibility of my data? Advantages to having all this structured “stuff” 12/8/2018 7:20 AM
11
12/8/2018 7:20 AM
12
12/8/2018 7:20 AM
13
Try-Catch (Another example)
14
12/8/2018 7:20 AM
15
Structured Exception Handlers (Syntax)
try // Place executable statements that might throw // (cause) an exception in this block. catch // This code runs if the statements in the Try // block throw an exception. finally // This code always runs immediately before // the Try block or Catch block exits. 12/8/2018 7:20 AM
19
What’s in a Try-Catch Statement?
The try block contains the code that may raise (throw) an exception or multiple exceptions The catch block contains the code that actually handles the exception thrown (additional catch blocks may handle additional exceptions) Note that if multiple procedures are called in the try block, the catch block catches all exceptions that are not handled by the called procedures Variables declared within a try … catch block are local to that block Code for the most specific exceptions must be placed first and for the least specific exceptions the code comes last 12/8/2018 7:20 AM
20
Execution Flow of a Structured Exception Handler (VB example)
22
Notes on the Last Slide Always catch code for the most specific exceptions first. Why? The Finally code block is executed whether the exception occurs and is caught or not The ToDecimal and ToInt32 methods (of the Convert class) do not do any rounding and can lead to FormatExceptions in certain cases This is as opposed to CDec and CInt, the intrinsic functions that do rounding (which can lead to type conversion errors) 12/8/2018 7:20 AM
23
Throwing Exceptions 12/8/2018 7:20 AM
24
A Function that Throws a FormatException
12/8/2018 7:20 AM
25
Throwing Exceptions for Special Purposes
12/8/2018 7:20 AM
26
When to Throw an Exception
12/8/2018 7:20 AM
27
Code To Validate One Data Entry Item
What data validation do you need to perform? Ensure that an entry has been made In other words ensure that the user has not skipped over the item Ensure that the entry is in the proper format Ensure that the entry range is valid Note use of the Select method for a Textbox Moves control back to the given Textbox Where parameters are used, Select also helps the user get things correct by showing what the incorrect entry See also, pp in text 12/8/2018 7:20 AM
28
Three Functions for Validating Textbox Entries
These functions are generic ONLY in the sense that they will work on any Textbox However, the functions that check for Decimal ranges and formats are not generic as to the types of the values whose ranges or formats are to be checked SO – beware – read the code on pp 12/8/2018 7:20 AM
29
Validating Multiple Entries
This material concerns the writing of a function to validate multiple (three in this case) textbox entries on a single form. You can take your choice – separate validations for each box or one per form (or worse, one long compound condition) Read pp in text 12/8/2018 7:20 AM
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.