Lecture 8: Exceptions and File I/O

This lecture considers some of the ways in which errors can be signalled by code and highlights their weaknesses. It introduces exceptions as a safer and more flexible approach, noting that they interrupt normal flow of control in a program and cannot be ignored. Examples of standard Java exceptions are presented and the difference between checked and unchecked exceptions is explained. Consideration is also given to how we intercept exceptions using try and catch blocks.

The second part of the lecture explores three different ways of reading from text files, and one way of writing formatted data to a text file. The lecture considers how we deal with the exceptions that may occur during file I/O. It also demonstrates the value of using a ’try with resources' block to ensure that files are closed automatically, regardless of whether an exception occurred.

Useful resources: