#catch/switch - error: incompatible types: 'fs.File' and 'void'
1 messages · Page 1 of 1 (latest)
const std = @import("std");
pub fn main() !void {
std.log.info("Starting Up\n", .{});
var database_file = std.fs.cwd().openFile("zig.db", .{}) catch |err| switch (err) {
error.FileNotFound => {
std.log.err("File Not Found\n", .{});
},
else => {
std.debug.panic("Panicked at Error: {any}", .{err});
},
};
defer database_file.close();
}
❯ zig build run
run
└─ run zigdb
└─ zig build-exe zigdb Debug native 1 errors
src/main.zig:12:62: error: incompatible types: 'fs.File' and 'void'
var database_file = std.fs.cwd().openFile("zig.db", .{}) catch |err| switch (err) {
^~~~~
src/main.zig:12:62: note: type 'fs.File' here
src/main.zig:13:31: note: type 'void' here
error.FileNotFound => {
^
referenced by:
callMain: /home/justin/zig/0.13.0/files/lib/std/start.zig:524:32
callMainWithArgs: /home/justin/zig/0.13.0/files/lib/std/start.zig:482:12
remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/home/justin/zig/0.13.0/files/zig build-exe -ODebug -Mroot=/home/justin/repos/zigdb/src/main.zig --cache-dir /home/justin/repos/zigdb/.zig-cache --global-cache-dir /home/justin/.cache/zig --name zigdb --listen=-
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zigdb transitive failure
├─ zig build-exe zigdb Debug native 1 errors
└─ install transitive failure
└─ install zigdb transitive failure
└─ zig build-exe zigdb Debug native (reused)
error: the following build command failed with exit code 1:
/home/justin/repos/zigdb/.zig-cache/o/b4c67704291554e28aa08115e20e91c9/build /home/justin/zig/0.13.0/files/zig /home/justin/repos/zigdb /home/justin/repos/zigdb/.zig-cache /home/justin/.cache/zig --seed 0x37c51abc -Zc9fafff51dee1d36 run
the catch branch has to return something of the same type
const a: i32 = some_errorable() catch 25; // if there is an error, a is 25
you probably want to return if there's an error I assume?
const a: i32 = some_errorable catch {
return;
};
the reason this doesn't work is because the catch branch is returning void
// this doesn't work
const file = openFile() catch {
std.log.err("File not found", .{});
// (code keeps executing after this point)
};
print(file); // if the file isn't found, what would this do?
with catch, you need to provide a value for the error case
const my_number = makeNumber() catch 25;
print(my_number); // if makeNumber fails, my_number is 25
or you need to return so the code doesn't continue beyond the point
const file = openFile() catch {
std.log.err("File not found", .{});
return;
};
print(file); // if the file isn't found, we have returned from the function so the code will never get here
try std.fs.cwd().createFile("zig.db", .{ .truncate = false });
https://ziglang.org/documentation/master/std/#std.fs.Dir.createFile
Sorry, I need to be able to catch the error to provide the "option" to create the file. I think my question is mostly, why isn't my catch/switch valid.
Sorry, I'm still not understanding.
If the file read fails, id like to be able to check if it's because it doesn't exist, and provide the option to create the file, if it's any other error I want to crash.
Do I need to return a fs.File in each section of that switch?
Effectively what they're getting at is that you just need to return the error in the else branch of your switch statement after your logging. This will end the whole function in an error, which is what I think you're going for.
Otherwise, the catch returns void implicitly to the original call location because you didn't return a value.