Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output.

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS 1 Copyright: FALL 2014 Illinois Institute of Technology_ George Koutsogiannakis.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
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.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS1101: Programming Methodology Aaron Tan.
Java Programming Robert Chatley William Lee
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
20 Oct - Overview Homework #1 Group-Id rule Notes on Java text file input/output –Scanner class –Printf (like C)
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 12: Programming Project.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
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.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Chapter 8-Exception Handling/ Robust Programming.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
Introduction Exception handling Exception Handles errors
Creating and Modifying Text part 2
Handling Exceptions.
Chapter 12 Exception Handling and Text IO
Unit 6 Working with files. Unit 6 Working with files.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
Session 2: Introduction to Object Oriented Programming
Java Basics Exception Handling.
CMSC 202 Exceptions.
Presentation transcript:

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output

Revision of session 10 Constructors are methods which are run when a class is instantiated. They have the same name as the class. The have no return type. Useful for assigning values to instance variables. Using constructors helps keep codes short – good programming style.

class Car { String colour; int doors; Car(String c, int d) { colour = c; doors = d; } Car fiesta = new Car(“silver”,5); Revision of session 10

Session 11 – aims & objectives Error handling. What happens if you have an error in your code? In many languages, the code just crashes or starts behaving wrongly. Java can help you detect errors and handle them properly! Learn how to read data from files. Learn how to write data to files.

Error handling Errors that arise while a program runs, are called run-time errors; these can cause an exception. Java will report these exceptions… … which may be trapped, identified and handled using try/catch statements. 'try' is a Java keyword that tests a block of code. 'catch' is another keyword that tells the program what to do if an exception occurs (is "thrown" by the program).

Example of an exception class ArrayError { public static void main (String[ ] args) { String sport [ ] = new String [3]; sport[0] = “Golf”; sport[1] = “Tennis”; sport[2] = “Squash”; System.out.println(sport[3]); }

Output of class ArrayError granby$ javac ArrayError.java granby$ java ArrayError Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 at ArrayError.main(ArrayError.java: 9) Type of exception Name of problem class Line number of problem

Using try/catch statements for(int i=5; i>=0; i--) { try { System.out.println(10.0/i); } catch(Exception e) { System.out.println(“Oh no! An error!”); }

Why bother error-handling? Java VM will report exceptions, so why should we bother? More user-friendly than a load of programming jargon Allows your program to continue once the error has been handled May allow your program to run with repeated errors

File Input/Output File I/O is done using the java.io package. Remember: import java.io.*; All I/O must be error-trapped. a file can only be opened inside a try statement. There are several techniques for file I/O – the one in the example code is simple, robust and moderately platform- independent. N.B. It assumes the file is a standard text file.

The RandomAccessFile class To open a file for reading/writing: RandomAccessFile myFile = new RandomAccessFile(filename, mode); File opening mode must be either “r” or “rw” “r”= read-only “rw” = read and write access An IOException occurs if you want to write to the file, and it is opened with a mode of “r” If the mode is “rw” and the file does not exist, then an attempt is made to create it An IOException is thrown if the name argument refers to a directory

Example code See example code: ex11_01: Illustration of an error condition ex11_02: Error handling ex11_03: Multiple exceptions ex11_04: File I/O (reading) using RandomAccessFile ex11_05: File I/O (writing) using RandomAccessFile Try today’s exercises...

Coming up in Session Programming project! Using all the concepts which we’ve covered in the course so far: Software design. Pseudocode. Coding, bit by bit. Software testing. Successive refinement.