Library contents

Scala 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

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

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

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.scala
import java.nio.file.Files
import java.nio.file.NoSuchFileException

object Lesson {
    def main(args: Array[String]): Unit = {
        val folder = Files.createTempDirectory("receipts")
        try {
            println(Files.readString(folder.resolve("today.txt")))
        } catch { case _: NoSuchFileException =>
            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