Exceptions Complicate Code

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 7 File I/O (and a little bit about exceptions)‏
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
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.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
A Few Exceptions, A Little IO, and Persistent Objects Rick Mercer.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Java I/O Writing and Reading Objects to File Serialization.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
XSLT in Practice. Exercises  download Apache Xalan - install it - try the example in Xalan-Java Overview  ZVON XSLT Tutorial.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Accessing Files in Java
Testing and Exceptions
Revision.
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.
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
YG - CS170.
CS 190 Lecture Notes: Exception Handling
CS 190 Lecture Notes: Exception Handling
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Unix File Interface int open(const char* path, int flags, mode_t permissions); ssize_t read(int fd, void* buffer, size_t count); ssize_t write(int fd,
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
CS 190 Lecture Notes: Tweeter Project
Exceptions Complicate Code
TRY CATCH BLOCK By Kosala Rajapaksha.
Test patterns.
Exception Handling.
Web Design & Development Lecture 7
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Computer Science and Engineering
Files Extra Example.
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Exceptions Complicate Code
Files and Streams in Java
Tutorial Exceptions Handling.
Exception Handling Contents
Exceptions.
Exceptions.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Tutorial MutliThreading.
Exceptions one last time…
Exceptions.
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
David Davenport Spring 2005
Presentation transcript:

Exceptions Complicate Code try ( FileInputStream fileStream = new FileInputStream(fileName); BufferedInputStream bufferedStream = new BufferedInputStream(fileStream); ObjectInputStream objectStream = new ObjectInputStream(bufferedStream); ) { for (int i = 0; i < tweetsPerFile; i++) { tf.tweets.add((Tweet) objectStream.readObject()); } catch (FileNotFoundException e) { ... catch (ClassNotFoundException e) { catch (EOFException e) { // Not a problem: not all tweet files have full set of tweets. catch (IOException e) { catch (ClassCastException e) { “Try with resources” CS 190 Lecture Notes: Exception Handling

Exceptions Complicate Code protected void closeStreams() { if (currentObjectStream == null) { return; } try { currentObjectStream.close(); catch (IOException e) { Log.printf("couldn't close ObjectOutputStream for '%s’”, currentFileName); currentFileStream.close(); Log.printf("couldn't close FileOutputStream for '%s’”, currentFileName); currentFileName = null; currentFileStream = null; currentObjectStream = null; CS 190 Lecture Notes: Exception Handling

Inadequate Error Messages } catch (IOException | ClassNotFoundException e) { Tweeter.finishWithError("Problem communicating " + "with filesystem."); } } catch (NumberFormatException e) { throw new IllegalArgumentException(paramKey + " must be a 64-bit integer"); “Try with resources” CS 190 Lecture Notes: Exception Handling