Lecture 11 Slides

Download Report

Transcript Lecture 11 Slides

Programming for Beginners
Lecture 11: Handling Errors; File Input/Output
Martin Nelson
Elizabeth FitzGerald
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.
Revision of session 10
class Car
{
String colour;
int doors;
Car(String c, int d)
{
colour = c;
doors = d;
}
}
Car fiesta = new Car(“silver”,5);
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”
Type of
exception
java.lang.ArrayIndexOutOfBoundsException: 3
at ArrayError.main(ArrayError.java: 9)
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 platformindependent.

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 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.