Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 10

The keyword Finally

In the last chapter (about streams), it was said that the streams must be closed once the program finishes working with them:

public static String myFirstLine(File myFile) throws IOException {
  BufferedReader myReader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile), "UTF-8"));
  String line = myReader.readLine();
  myReader.close();
  return line;
}

This code is not ideal. If the readLine method causes an exception, the myFirstLine method will be stopped and the program will never close the stream (close).

In such programs, we want some code to be executed regardless of whether an exception occurs or is caught. Java has a finally clause that can be used to accomplish this objective. The finally block is added after the try block and its body is always executed - after the try has finished it work (even if it contains the return statement) or try block is stopped.

The correct version of the code above:

public static String myFirstLine(File myFile) throws IOException {
  BufferedReader myReader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile), "UTF-8"));
  try {
    return myReader.readLine();
  } finally {
    myReader.close(); // this block is executed even if the try block causes an exception
  }
}

The same result can be achieved using the try-with-resources syntax:

public static String myFirstLine(File myFile) throws IOException {
  try (BufferedReader myReader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile), "UTF-8"))) {
    return myReader.readLine();
  }
  // the myReader stream is closed even if the try block causes an exception 
}

Try-with-resources syntax forces the compiler generate automatically the finally block after the try block.

Try-with-resources, finally and catch can also be combined in the program:

try (InputStream is = new FileInputStream("myFile.txt")) {
  ..
  return result;
} catch (IOException e) {
  // if the IOException exception is thrown
} catch (RuntimeException e) {
  // if the RuntimeException exception is thrown
} finally {
  // this block is executed anyway
}

If an object must be closed (e.g. streams), use try-with-resources! This is the easiest and the most secure way to close objects. Try-with-resources must become a habit because we will need it all the time. Forgetting to close the streams is a typical mistake. Check Task 1.

Chapter 10
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment