Lecture 4: Exceptions. I/O

Similar documents
Inheritance E, xc Ex eptions ceptions I/O

I/O STREAM (REQUIRED IN THE FINAL)

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Exception Handling CSCI 201 Principles of Software Development

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Darshan Institute of Engineering & Technology for Diploma Studies

Lecture 11.1 I/O Streams

CSPP : Introduction to Object-Oriented Programming

Input from Files. Buffered Reader

Java Input/Output. 11 April 2013 OSU CSE 1

7. Java Input/Output. User Input/Console Output, File Input and Output (I/O)

BBM 102 Introduction to Programming II Spring Exceptions

PIC 20A Streams and I/O

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Lab 2: File Input and Output

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Byte and Character Streams. Reading and Writing Console input and output

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Object-Oriented Programming Design. Topic : Streams and Files

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Project #1 Computer Science 2334 Fall 2008

Each command-line argument is placed in the args array that is passed to the static main method as below :

CS 201, Fall 2016 Sep 28th Exam 1

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

BBM 102 Introduction to Programming II Spring 2017

IT101. File Input and Output

Lesson 3: Accepting User Input and Using Different Methods for Output

Lecture 19 Programming Exceptions CSE11 Fall 2013

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Input & Output in Java. Standard I/O Exception Handling

Software Practice 1 - File I/O

Recitation 9 Stream and Phidget

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions. What exceptional things might our programs run in to?

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Internet Technology 2/7/2013

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Exception Handling. Content 25/03/15. Advanced Programming

Exceptions and Libraries

Midterm Logistics. Midterm tonight, 7PM 10PM Exam location by last name:

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Networking Code CSCI 201 Principles of Software Development

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

9. Java Errors and Exceptions

What are Exceptions?

1.00 Lecture 30. Sending information to a Java program

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

CS11 Java. Fall Lecture 4

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Exceptions Handling Errors using Exceptions

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Java Programming Unit 7. Error Handling. Excep8ons.

Shell Interface Assignment

Sri Vidya College of Engineering & Technology Question Bank

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

CS 2113 Software Engineering

Exceptions - Example. Exceptions - Example

Introduction Unit 4: Input, output and exceptions

Here is a hierarchy of classes to deal with Input and Output streams.

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

WOSO Source Code (Java)

CS 251 Intermediate Programming Java I/O Streams

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

ITI Introduction to Computing II

Writing your own Exceptions. How to extend Exception

ITI Introduction to Computing II

Introduction to Software Engineering in Java. Exceptions, I/O, and you++

Defensive Programming. Ric Glassey

Objec-ves JAR FILES. Jar files. Excep-ons. Files Streams. Ø Wrap up Ø Why Excep-ons? 9/30/16. Oct 3, 2016 Sprenkle - CSCI209 1

CN208 Introduction to Computer Programming

CS Programming I: File Input / Output

Exceptions and Working with Files

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

ArrayList, Hashtables, Exceptions, and Files

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

Simple Java Input/Output

FILE I/O IN JAVA. Prof. Chris Jermaine

Object Oriented Programming. Week 9 Part 1 File I/O

CS Week 11. Jim Williams, PhD

EXCEPTIONS. Fundamentals of Computer Science I

CSC 1214: Object-Oriented Programming

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exceptions. Examples of code which shows the syntax and all that

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

More on Exception Handling

CS-152: Software Testing

Transcription:

Lecture 4: Exceptions. I/O

Outline Access control. Class scope Exceptions I/O

public class Malicious { public static void main(string[] args) { maliciousmethod(new CreditCard()); } static void maliciousmethod(creditcard card) { card.expenses = 0; System.out.println(card.cardNumber); } }

Меthod C

Меthod public class ScopeReview { void scopemethod(int var1) { String var2; if (var1 > 0) { var2 = "above 0"; } else { var2 = "less than or equal to 0"; } System.out.println(var2); } }

public class ScopeReview { private int var3; void scopemethod(int var1) { var3 = var1; String var2; if (var1 > 0) { var2 = "above 0"; } else { var2 = "less than or equal to 0"; } System.out.println(var2); } }

public class Baby { int servings; void feed(int servings) { servings = servings + servings; } void poop() { System.out.println("All better!"); servings = 0; } }

public class Baby { int servings; void feed(int servings) { this.servings = this.servings + servings; } void poop() { System.out.println("All better!"); servings = 0; } }

Outline Access control. Class scope Exceptions I/O

Exceptions NullPointerException ArrayIndexOutOfBoundsException ClassCastException RuntimeException

What is an Exception? Event that occurs when something unexpected happens null.somemethod(); (new int[1])[1] = 0; int i = string ;

Why use an Exception? To tell the code using your method that something went wrong Exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 at RuntimeException.main(RuntimeException.java:8) Accessed index 5, which isn t in the array The method that called it was main Debugging and understanding control flow

How do exceptions happen? Java doesn t know what to do, so it Creates an Exception object Includes some useful information throws the Exception You can create and throw Exceptions too!

public class Exception Exception is a class Just inherit from it! public class MyException extends Exception { } Or use existing ones http://rymden.nu/exceptions.html

Warn Java about the Exception public Object get(int index) throws ArrayOutOfBoundsException { } If (index < 0 index >= size()) throw new ArrayOutOfBoundsException( +index); throws tells Java that get may throw the ArrayOutOfBoundsException throw actually throws the Exception (sorry)

Catching an Exception Java now expects code that calls get to deal with the exception by Catching it Rethrowing it

Catching it What it does try to run some code that may throw an exception Tell Java what to do if it sees the exception (catch) try { get(-1); } catch (ArrayOutOfBoundsException err) { System.out.println( oh dear! ); }

Rethrowing it Maybe you don t want to deal with the Exception Tell Java that your method throws it too void dobad() throws ArrayOutOfBoundsException { } get(-1);

main Rethrowing it

Rethrowing it main dobad

Rethrowing it main dobad get

Rethrowing it main dobad get

Rethrowing it main dobad get Exception

Rethrowing it main dobad get Exception

Rethrowing it main dobad Exception

Rethrowing it main dobad Exception

Rethrowing it main Exception

Rethrowing it main Exception

What it no one catches it? If you ran public static void main(string[] args) throws Exception { dobad(); } Java will print that error message you see Exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 at YourClass.get(YourClass.java:50) at YourClass.doBad(YourClass.java:11) at YourClass.main(YourClass.java:10)

Outline Access control. Class scope Exceptions I/O

We ve seen Output System.out.println( some string );

The Full Picture Hard drive Network 100101010101000101... InputStream System.in O k a y a w e InputStreamReader Okay awesome, cool\n BufferedReader

InputStream InputStream is a stream of bytes Read one byte after another using read() A byte is just a number Data on your hard drive is stored in bytes Bytes can be interpreted as characters, numbers.. InputStream stream = System.in;

InputStreamReader Reader is a class for character streams Read one character after another using read() InputStreamReader takes an InputStream and converts bytes to characters Still inconvenient Can only read a character at a time new InputStreamReader(stream)

BufferedReader BufferedReader buffers a character stream so you can read line by line String readline() new BufferedReader( new InputStreamReader(System.in));

User Input InputStreamReader ir = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ir); br.readline();

FileReader FileReader takes a text file converts it into a character stream FileReader( PATH TO FILE ); Use this + BufferedReader to read files! FileReader fr = new FileReader( readme.txt ); BufferedReader br = new BufferedReader(fr);

FileReader Code import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; public class ReadFile { public static void main(string[] args) throws IOException{ // Path names are relative to project directory (Eclipse Quirk ) FileReader fr = new FileReader("./src/readme"); BufferedReader br = new BufferedReader(fr); String line = null; while ((line = br.readline())!= null) { System.out.println(line); } br.close(); } }