IOError
The IOError type provides structured error handling for file and I/O operations.
Used by File module functions like File.read,
File.write, File.append, and File.delete.
import IO
match File.read "config.txt"
| Ok content -> process content
| Err FileNotFound{path} -> println "File not found: ${path}"
| Err AccessDenied{path} -> println "Access denied: ${path}"
| Err e -> println "Error: ${Error.message e}"
See Also: Contracts
For preventing errors through preconditions and postconditions, see
Contracts. Use @pre
to validate inputs before operations that might fail.
Constructors
IOError
type IOError = FileNotFound{...} | AccessDenied{...} | ...
| Constructor | Fields | Description |
|---|---|---|
FileNotFound |
path: String |
The specified file does not exist |
AccessDenied |
path: String |
Permission denied for the operation |
IsDirectory |
path: String |
Expected a file but found a directory |
NotDirectory |
path: String |
Expected a directory but found a file |
ReadError |
message: String |
Error while reading |
WriteError |
message: String |
Error while writing |
DeleteError |
message: String |
Error while deleting |
OutOfMemory |
— | Insufficient memory for operation |
IOError |
message: String |
Generic I/O error |
Trait Implementations
Show
Provides human-readable error descriptions:
err = FileNotFound{path: "/tmp/missing.txt"}
println (show err)
# => FileNotFound: /tmp/missing.txt
Error
Implements the standard Error trait with message and kind:
| Constructor | Error Kind |
|---|---|
FileNotFound | :file-not-found |
AccessDenied | :access-denied |
IsDirectory | :is-directory |
NotDirectory | :not-directory |
ReadError | :read-error |
WriteError | :write-error |
DeleteError | :delete-error |
OutOfMemory | :out-of-memory |
IOError | :io-error |
Examples
Reading a File with Error Handling
read-config = fn(path) =>
match File.read path
| Ok content -> Ok (parse-config content)
| Err FileNotFound{..} -> Ok default-config
| Err AccessDenied{path} ->
println "Warning: Cannot read ${path}, using defaults"
Ok default-config
| Err e -> Err e
Handling by Error Kind
handle-error = fn(err) =>
match Error.kind err
| :file-not-found -> println "File missing"
| :access-denied -> println "Permission denied"
| _ -> println "I/O error: ${Error.message err}"