Library contents

Groovy NoSuchFileException

Open a receipt in a fresh workspace and handle the case where nobody has saved it yet.

A simple example

NoSuchFileException

  1. Start with No file named a.txt
  2. Do this Open a.txt for reading
  3. Result File missing
The missing-path outcome lets the caller offer an empty starting state or ask for another name.

Syntax

Groovy syntax
Files.readString(path)
Run these examples in Groovy

Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.

groovy Lesson.groovy

Recover when a receipt is missing

A clerk requests a receipt before a new folder contains any exports. Handle that missing entry explicitly so the application can give the clerk a useful next action.

ResultNo receipt yet
Lesson.groovy
import java.nio.file.Files
import java.nio.file.NoSuchFileException

def folder = Files.createTempDirectory("receipts")
try {
    println(Files.readString(folder.resolve("today.txt")))
} catch (NoSuchFileException missing) {
    println("No receipt yet")
} finally {
    Files.deleteIfExists(folder)
}
Output
No receipt yet

The example deliberately uses an absent receipt. Permission failures and unrelated errors should still be visible.

Advertisements

Read a saved shift record

The closing clerk saved the file before leaving. Open the expected file and show the desk whether it is available.

Givenrecord.txt exists
Worked resultFile available

Handle an unsubmitted handover

The next shift opens an empty inbox before the first clerk submits a file. Open the expected file and show the desk whether it is available.

Givenrecord.txt missing
Worked resultAsk for the file

What to watch for

An existence check and a later open are separate operations. Handle failure at the point where the file is opened.

Documentation for NoSuchFileException