Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 1d0baef

Browse files
authored
Gracefully exit instead of crashing when a file is not found. (#144)
Use `print(_:)` and `exit(_:)` instead of a `fatalError(_:file:line:)` so that the program won't crash when a file doesn't exist.
1 parent 84d741a commit 1d0baef

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

Autoencoder/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func readFile(_ filename: String) -> [UInt8] {
5757
let d = Python.open(filePath, "rb").read()
5858
return Array(numpy: np.frombuffer(d, dtype: np.uint8))!
5959
}
60-
fatalError(
61-
"Failed to find file with name \(filename) in the following folders: \(possibleFolders).")
60+
print("Failed to find file with name \(filename) in the following folders: \(possibleFolders).")
61+
exit(-1)
6262
}
6363

6464
/// Reads MNIST images and labels from specified file paths.

MNIST/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func readFile(_ path: String) -> [UInt8] {
2727
let data = try! Data(contentsOf: filePath, options: [])
2828
return [UInt8](data)
2929
}
30-
fatalError("Filename not found: \(path)")
30+
print("File not found: \(path)")
31+
exit(-1)
3132
}
3233

3334
/// Reads MNIST images and labels from specified file paths.

MiniGo/main.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ let modelConfig = ModelConfiguration(boardSize: boardSize)
3030
var model = GoModel(configuration: modelConfig)
3131

3232
guard FileManager.default.fileExists(atPath: "./MiniGoCheckpoint/000939-heron.data-00000-of-00001")
33-
else { fatalError("Please download the MiniGo checkpoint according to the README.md.") }
33+
else {
34+
print("Please download the MiniGo checkpoint according to the README.md.")
35+
exit(-1)
36+
}
3437
let reader = PythonCheckpointReader(path: "./MiniGoCheckpoint/000939-heron")
3538
model.load(from: reader)
3639

0 commit comments

Comments
 (0)