Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
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.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
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.
Finding and Debugging Errors
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Exceptions COMPSCI 105 S Principles of Computer Science.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
Chapter 12: Exception Handling
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
File I/O 11_file_processing.ppt
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
VB.Net - Exceptions Copyright © Martin Schray
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Introduction to Exception Handling and Defensive Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Files Tutor: You will need ….
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Error Trapping Exception Catching 1. Syntax & Compile-time  VB – Editor/Compiler Logic and Design  You Runtime  You Types of Errors: 2 When your program.
Controlling Program Flow with Decision Structures.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
PRACTICAL COMMON LISP Peter Seibel 1.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Java Programming Fifth Edition
Part IX Fundamentals of C and C++ Programming Exception Handling
Files and Streams Lect3 CT1411.
Advanced Java Programming
Topics Introduction to File Input and Output
Part B – Structured Exception Handling
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Chapter 11: Exception Handling
Presentation transcript:

Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1

CO331 Visual Programming Sequential files These are the most basic type of file that generally holds human readable text. Accordingly the contents of such files may be read using, for example, Notepad. A sequential file stores items of data one after another; each new item of data being added to the end of the data already stored. A sequential file is used, usually, when Its not expected to make changes to the data within the file too often The file information is likely to be processed from start to finish without the need to jump about within the file to acquired particular items of data. It is acceptable to add new data to the end of the file. 2

CO331 Visual Programming File class Programs that involve file processing must include Imports System.IO at the beginning. The methods of File class: OpenText is used to open an existing file E.g. If the file does not exist then a runtime error will occur. CreateText is used to create a new text file or overwrite an existing file. E.g. 3 File.CreateText (“U:\home\units\co331\aNewFile.txt”) File.OpenText(“U:\home\units\co331\aOldFile.txt”)

CO331 Visual Programming Streams The term ‘stream’ is used in the sense of a stream of data flowing in or out of a program. To process data in an existing file it is necessary to:  Open the file  Read (input) the data item-by-item from the file into variables  Close the file on completion To transfer data from variables into a file, it is necessary to:  Open the file  Output (write) individual items in the required sequence to the file  Close the file on completion 4

CO331 Visual Programming StreamReader Class -will be used to read lines of text from a file. The ReadLine method of StreamReader reads a whole line of text into a string, excluding the end-of-line marker. E.g. 5

CO331 Visual Programming StreamWriter Class The StreamWriter class has two main methods: Write, and WriteLine Both write a string to file, but WriteLine adds an end-of-line marker after the string. WriteLine can also be used with no argument to terminate a line with an end-of-line marker. E.g. 6

CO331 Visual Programming File searching Searching a file for an item that satisfies some condition is quite common. E.g. Suppose we have a file of examination marks obtained by named students: Smith, 43, 67 Jones, 87, 99 Patel, 54, 32 Sutton, 65, 74 etc… and we wish to obtain the marks of a particular student from the file and display them. 7

CO331 Visual Programming File searching - example The process may be expressed in structured English as: 8 found = False While (more lines to read) And found = False read line split line into three fields If first field matches name Then found = True display other fields in labels End If End While If Not found Then display a warning End If

CO331 Visual Programming E.g. File searching - Code Ensure appropriate input data present: 9

CO331 Visual Programming E.g. File searching - Code Continuing: 10 Run Hangman

CO331 Visual Programming Error handling - Exceptions Is designed to deal with run-time errors such as an attempt to divide by zero (integer division) where memory becomes exhausted an out-of-bounds array index arithmetic overflow Error handling should be used to process only exceptional situations recover from infrequent fatal errors safeguard data and exit a program neatly provide some indication of the fault that has arisen In general program control using conventional control structures is more efficient than using error handling facilities but the main logic might become obscured. 11

CO331 Visual Programming Exceptions – cont’d Exception is a term used in VB to indicate something has gone wrong. Exceptions are created by being thrown, and are detected by being caught. VB uses the keywords Throw, Try and Catch to handle these operations. E.g. Try and Catch 12 Private Sub Calculate_Click(…………….)Handles Calculate.Click Dim side As Double Try side = Double.Parse(txtSide.Text) lblDisplay.Text = “Area is ” & CStr(side * side) & “ sq units” Catch exceptionObject As FormatException lblDisplay.Text = “Error in side: re-enter” End Try End Sub

CO331 Visual Programming Exceptions – cont’d Once an exception has been thrown it is possible to provide additional information by examining the exception object’s Message and using theToString Method. E.g. The message property provides a short string containing a descriptive error message. The ToString method returns a string containing several lines providing a trace of the actions that led up to the exception including the location of where the error occurred. 13 Catch exceptionObject As FormatException MessageBox.Show(exceptionObject.Message) MessageBox.Show(exceptionObject.ToString())

CO331 Visual Programming E.g. Error handling In the case of division by zero by floating-point division, VB.Net outputs the word ‘infinity’. 14

CO331 Visual Programming Files and exceptions File input-output is a major source of exceptions, e.g. incorrect file name disk full CD removed while reading in progress The File.OpenText method can throw a number of exceptions. In particular FileNotFoundException. E.g. Program 2 may be modified to include 15 Catch problem As FileNotFoundException MessageBox.Show(“Error - “ & txtFile.Text & _ “ file not found:. Re-enter name.”) Catch problem As Exception MessageBox.Show(“Error – concerning file: “ & _ txtFile.Text & “. “ & problem.message()) End Try