Part B – Structured Exception Handling

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exceptions COMPSCI 105 S Principles of Computer Science.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
Object Oriented Programming
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exceptions Handling Exceptionally Sticky Problems.
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Murach’s Visual Basic 2008, C7, modified or added© 2008, Mike Murach & Associates, Inc. Slide 1.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
C++ Exceptions.
Handling Exceptionally Sticky Problems
Java Programming Language
Computer Programming I
CSE 501N Fall ’09 17: Exception Handling
Chapter 14: Exception Handling
Exceptions 10-Nov-18.
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
Topics Introduction to File Input and Output
Exceptions & Error Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Fundamental Error Handling
Exception Handling By: Enas Naffar.
Programming in C# Lesson 5. Exceptions..
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Lecture 11 Objectives Learn what an exception is.
Microsoft Visual Basic 2005 BASICS
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Handling Exceptionally Sticky Problems
Exceptions 10-May-19.
Debugging and Handling Exceptions
Topics Introduction to File Input and Output
Java Basics Exception Handling.
Chapter 11: Exception Handling
CMSC 202 Exceptions 2nd Lecture.
Exception Handling and Event Handling
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Part B – Structured Exception Handling Lecture Set 8 Validating Data and Handling Exceptions Part B – Structured Exception Handling

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

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

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

The Exception Class Hierarchy 12/8/2018 7:20 AM

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

12/8/2018 7:20 AM

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

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

12/8/2018 7:20 AM

12/8/2018 7:20 AM

Try-Catch (Another example)

12/8/2018 7:20 AM

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

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

Execution Flow of a Structured Exception Handler (VB example)

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

Throwing Exceptions 12/8/2018 7:20 AM

A Function that Throws a FormatException 12/8/2018 7:20 AM

Throwing Exceptions for Special Purposes 12/8/2018 7:20 AM

When to Throw an Exception 12/8/2018 7:20 AM

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. 200-203 in text 12/8/2018 7:20 AM

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. 202-205 12/8/2018 7:20 AM

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. 204-209 in text 12/8/2018 7:20 AM