Groovy NoSuchFileException
Open a receipt in a fresh workspace and handle the case where nobody has saved it yet.
A simple example
NoSuchFileException
- Start with No file named a.txt
- Do this Open a.txt for reading
- Result File missing
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.groovyRecover 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.
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)
}
No receipt yet
The example deliberately uses an absent receipt. Permission failures and unrelated errors should still be visible.
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.
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.
What to watch for
An existence check and a later open are separate operations. Handle failure at the point where the file is opened.