Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.

Slides:



Advertisements
Similar presentations
Yoshi
Advertisements

Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
COMP 121 Week 5: Exceptions and Exception Handling.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
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.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
Files and Streams CS 21a Chapter 11 of Horstmann.
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.
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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
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.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
COMP Exception Handling Yi Hong June 10, 2015.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
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.
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.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Winter 2006CISC121 - Prof. McLeod1 Last Time Wrapper classes JFileChooser (along with JOptionPane, and JColorChooser !) Text File Output.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
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.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Last Time Reviewed method overloading. A few useful Java classes:
Creating and Modifying Text part 2
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Topics Introduction to File Input and Output
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Presentation transcript:

Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output (concentrate on text I/O) Winter 2015CMPE212 - Prof. McLeod1

Pointers – A Question So, which way is better to declare a 3 by two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; Or, it makes no difference? Winter 2015CMPE212 - Prof. McLeod2

How Much Memory Does a Pointer Use? String aString = “Hello!”; How much memory does aString consume? Not the string itself, just the pointer to the string. 32 bits for 32 bit Java, 64 bits for 64 bit Java (unless you are using compressed pointers…) Winter 2015CMPE212 - Prof. McLeod3

null Pointer or null Reference null is not a keyword in Java – more like a literal constant. What is a null pointer? What is a null pointer error? Does null have a type? Can you test a pointer to see if it is null ? How? Why would you want to? Winter 2015CMPE212 - Prof. McLeod4

null Pointer or null Reference, Cont. What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; See TestNull.java. Is an unassigned primitive type variable null ? What are the contents of an uninitialized array? Winter 2015CMPE212 - Prof. McLeod5

null References, Cont. The idea of a null reference was first introduced into ALGOL W back in 1965 by C.A.R. Hoare (also known as the inventor of Quicksort). See: References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2015CMPE212 - Prof. McLeod6

Using null in Java You can test to see if a pointer is null (See file input code, for example. This is how you detect the end of the file.) Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. In this case you must assign the variable pointer to null. “Bad things” will happen if you do not! A NullPointerException is probably the most frustrating error to encounter in Java! Winter 2015CMPE212 - Prof. McLeod7

Fall 2014CISC124 - Prof. McLeod8 File I/O Files provide a convenient way to store and re- store to memory larger amounts of data. Three kinds of file I/O: –Text –Binary –Random access

File I/O, Cont. Text –ASCII characters written to a text file (*.txt for example) that is readable by a text editor such as Notepad. –Text files can be used to interface with other programs like Word and Excel (use *.csv, for example). Binary –Stored exactly as in memory, in binary. –Not readable with a text editor. Random Access –Like Binary, but can read and write any byte in the file in any order. Fall 2014CISC124 - Prof. McLeod9

File I/O and Exceptions Since you are “reaching out” through the OS to read and write files, errors may occur that Java cannot do anything about. Such as: –When reading: file does not exist. –When reading: no permission to read file. –When reading: part of file is corrupt. –When writing: folder cannot be found. –When writing: no permission to write to folder. –When writing: failure during write. –Etc.! Fall 2014CISC124 - Prof. McLeod10

File I/O and Exceptions, Cont. Most file I/O methods will indicate that one of these kinds of errors has been encountered by throwing an exception and then halting execution. If not caught, an exception will cause your program to crash. Fall 2014CISC124 - Prof. McLeod11

Java 7 File I/O or “NIO” Best tutorial and reference: In older Java versions files could not be closed properly if an exception was thrown leading to escalating problems and a program crash. In Java 7+ file closing takes place “behind the scenes” and files should always be closed properly. Fall 2014CISC124 - Prof. McLeod12

Problem with Java <7 File I/O Exceptions How to close the file when things are going wrong? // Declare fileWriter object = null try { // instantiate the fileWriter object } catch (IOException e) { // try to do something about the problem? } finally { // executes whether an exception is thrown or not fileWriter.close(); // but this can throw an // exception! } Fall 2014CISC124 - Prof. McLeod13

Fall 2014CISC124 - Prof. McLeod14 Exceptions – Cont. Syntax of a Java 7 “try-with-resources block”: try (instantiation; instantiation; …) { // other statements that might // generate an exception }[ catch (exception_type identifer) { // block of statements }][ catch (exception_type identifer) { // block of statements … }][ finally { // block of statements }]

Try-With-Resources Syntax The instantiation(s) inside the set of ( ) immediately after the try keyword are declared resources that must be local to the try/catch block. Note that there is no ; at the end of the list and that there does not have to be any catch blocks. These resources must all implement the AutoCloseable interface, which means that the try block can close the resource when it is finished. Resources will be closed whether or not an exception is thrown because their scope is forced to be in the try block only. As a result, all use of the resource must also take place in the try block. Fall 2014CISC124 - Prof. McLeod15

Fall 2014CISC124 - Prof. McLeod16 Exceptions – Cont. Text file opening example in older Java versions: FileReader fileIn = null; try{ fileIn = new FileReader(filename); } catch (FileNotFoundException e) { System.out.println("Cannot open file!"); System.exit(0); } Scanner fileInput = new Scanner(fileIn); Then you would have more try/catch blocks for reading and then closing the file.

Fall 2014CISC124 - Prof. McLeod17 Exceptions – Cont. Note that you can use the code e.getMessage() in the catch block to get an error description string from the exception inside the catch clause. When used with some exceptions, the e.getMessage() method just returns null, as they do not contain a message. (You can also get a stack dump, but this is not too useful…)

Java 7 File Open and Read Example try (BufferedReader reader = Files.newBufferedReader(file, charset)) { do { line = reader.readLine(); if (line != null) textArray[lineCount] = line; lineCount++; } while (line != null); } catch (IOException err) { System.out.println("Unable to read file: " + filename); System.exit(0); } Fall 2014CISC124 - Prof. McLeod18

Java 7 File Read – Cont. In this case everything is carried out in the try block. That’s because the resource ( reader on the previous slide) is scoped only inside the try block – so you have to do everything there – you have no choice! As soon as you move outside the try block (outside the scope of the resource) the resource is closed automatically. Fall 2014CISC124 - Prof. McLeod19

Java 7 File Read – Cont. Where do the file and charset objects come from? Before the try-with-resources block: Charset charset = Charset.forName("US-ASCII"); Path file = Paths.get(filename); Charset is not too exciting and we will talk more about the Java 7 Path object later. Fall 2014CISC124 - Prof. McLeod20