Skip to content

86 Typed file failures ​

After the previous lesson has claimed note.txt, remove only that file. A read of the same path now throws FileException; its typed reason identifies NotFound.

norm
import std.filesystem.FileException
import std.filesystem.Path
import std.filesystem.readText
import std.io.TextEncoding

main() {
  try {
    readText(path: Path(value: "note.txt"), encoding: TextEncoding.Utf8, maximumBytes: 1024)
  } catch FileException error {
    printLine(error.reason)
  }
}

If you are still in the scratch directory from lesson 85, run:

sh
rm note.txt
norm run ../../norm/tests/docs/libraries/files/missing.norm
cd ../..

You can also run the source independently from any new empty scratch directory. Never remove a file you did not create for this lesson.

Expected output:

text
FileFailure.NotFound

Try it: Run the read before removing note.txt and observe that no exception branch executes.

Precise rules: Filesystem failures.

Norm 0.25