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

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

Embed Size (px)

Citation preview

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

Programming for Programming for BeginnersBeginners

Martin Nelson

Elizabeth FitzGerald

Lecture 11: Handling Errors; File Input/Output

Page 2: 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.

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

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

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

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.

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

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).

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

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]);

}

}

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

Output of class ArrayError

granby$ javac ArrayError.javagranby$ java ArrayError

Exception in thread “main”

java.lang.ArrayIndexOutOfBoundsException: 3at ArrayError.main(ArrayError.java: 9)

Type of exception

Name of problem class Line number of problem

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

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!”);

}

}

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

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

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

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.

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

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

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

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...

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

Coming up in Session 12... 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.