1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.

Slides:



Advertisements
Similar presentations
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Advertisements

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.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
© The McGraw-Hill Companies, 2006 Working with files Chapter 20.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CSC – Java Programming II Lecture 9 January 30, 2002.
CIS 270—Application Development II Chapter 13—Exception Handling.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
CIS 270—Application Development II Chapter 14—Files and Streams.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
Files and Streams Chapter What You Will Learn Create files Read files Write files Update files.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Data Storage © 2007 Pearson Addison-Wesley. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
 Pearson Education, Inc. All rights reserved Files and Streams.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Data Storage © 2007 Pearson Addison-Wesley. All rights reserved.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
 Pearson Education, Inc. All rights reserved Files and Streams.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
CSE 1341 File I/O Exception Handling. Introduction to File Processing Data stored in variables and arrays is temporary. Main Memory Main Memory Secondary.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.
17 File Processing.
Ch14 Files and Streams OBJECTIVES
University of Central Florida COP 3330 Object Oriented Programming
I/O Basics.
Chapter 13: File Input and Output
Streams and File I/O Chapter 14.
JAVA IO.
Reading and Writing Files
MSIS 670: Object-Oriented Software Engineering
Workshop for Programming And Systems Management Teachers
Input and Output Stream
Workshop for Programming And Systems Management Teachers
14 Files and Streams.
Chapter 15 Files, Streams and Object Serialization
Presentation transcript:

1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling – Process exceptions from program components – Handle exceptions in a uniform manner in large projects – Remove error-handling code from “main line” of execution

1 Exceptions A method detects an error and throws an exception – Exception handler processes the error – Uncaught exceptions yield adverse effects Might terminate program execution

1 Try-Catch Code that could generate errors put in try blocks – Code for error handling enclosed in a catch clause – The finally clause always executes Termination model of exception handling – The block in which the exception occurs expires throws clause specifies exceptions method throws

1 Files Long-term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices – Magnetic disks – Optical disks – Magnetic tapes Sequential and random access files

1 Data Hierarchy Smallest data item in a computer is a bit – Bit can be either 0 or 1 – Bit short for “binary digit” Programmers work with higher level data items – Decimal digits: (0-9) – Letters: (A-Z and a-z) – Special symbols: (e.g., %, &, *, (, ), -, +, “, :, ?, /, etc.) – Java uses Unicode characters composed of 2 bytes A byte is 8 bits long Fields (Java instance variables) – Composed of characters or bytes – Conveys meaning

1 Data Hierarchy Data hierarchy – Data items in a computer form a hierarchy Progresses from bits, to characters, to fields, etc. Records – Composed of several fields – Implemented as a class in Java – See Fig for example File is a group of related records – One field in each record is a record key Record key is a unique identifier for a record – Sequential file Records stored in order by record key

1 Data Hierarchy RandyRed J u d y Green SallyBlack TomBlue JudyGreen IrisOrange File Record Field Byte (ASCII character J) Bit

1 Files and Streams Java views a file as a stream of bytes (Fig. 17.2) – File ends with end-of-file marker or a specific byte number – File as a stream of bytes associated with an object Java also associates streams with devices – System.in, System.out, and System.err – Streams can be redirected

1 Files and Streams File processing with classes in package java.io – FileInputStream for byte-based input from a file – FileOutputStream for byte-based output to a file – FileReader for character-based input from a file – FileWriter for character-based output to a file

1 Files and Streams Java views a file as a stream of bytes (Fig. 17.2) – File ends with end-of-file marker or a specific byte number – File as a stream of bytes associated with an object Java also associates streams with devices – System.in, System.out, and System.err – Streams can be redirected

1 Files and Streams File processing with classes in package java.io – FileInputStream for byte-based input from a file – FileOutputStream for byte-based output to a file – FileReader for character-based input from a file – FileWriter for character-based output to a file

1 Java’s view of a file of n bytes n-1 end-of-file marker 67

1 Files and Streams Buffering – Improves performance of I/O – Copies each output to a region of memory called a buffer – Entire buffer output to disk at once One long disk access takes less time than many smaller ones – BufferedOutputStream buffers file output – BufferedInputStream buffers file input

1 Class File – Provides useful information about a file or directory – Does not open files or process files Fig lists some useful File methods