Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output."— Presentation transcript:

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

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

3 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

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

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

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

7 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

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

9 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

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

11 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

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

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


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

Similar presentations


Ads by Google