From d12d5a677c14ac902994bf77f8a264b8b30f441c Mon Sep 17 00:00:00 2001 From: ganome Date: Mon, 27 Jan 2025 19:54:57 -0700 Subject: [PATCH] Finished quiz 4 - dealing with errors --- exercises/021_errors.zig | 4 ++-- exercises/022_errors2.zig | 3 +-- exercises/023_errors3.zig | 4 ++-- exercises/024_errors4.zig | 8 +++++++- exercises/025_errors5.zig | 4 +--- exercises/026_hello2.zig | 2 +- exercises/027_defer.zig | 2 +- exercises/028_defer2.zig | 2 +- exercises/029_errdefer.zig | 2 +- exercises/030_switch.zig | 3 +++ exercises/031_switch2.zig | 3 +++ exercises/032_unreachable.zig | 3 +++ exercises/033_iferror.zig | 1 + exercises/034_quiz4.zig | 10 ++++++++-- 14 files changed, 35 insertions(+), 16 deletions(-) diff --git a/exercises/021_errors.zig b/exercises/021_errors.zig index 7afeace..a6670b1 100644 --- a/exercises/021_errors.zig +++ b/exercises/021_errors.zig @@ -9,7 +9,7 @@ // "TooSmall". Please add it where needed! const MyNumberError = error{ TooBig, - ???, + TooSmall, TooFour, }; @@ -26,7 +26,7 @@ pub fn main() void { if (number_error == MyNumberError.TooBig) { std.debug.print(">4. ", .{}); } - if (???) { + if (number_error == MyNumberError.TooSmall) { std.debug.print("<4. ", .{}); } if (number_error == MyNumberError.TooFour) { diff --git a/exercises/022_errors2.zig b/exercises/022_errors2.zig index 1d513b3..5ac06ab 100644 --- a/exercises/022_errors2.zig +++ b/exercises/022_errors2.zig @@ -19,8 +19,7 @@ const std = @import("std"); const MyNumberError = error{TooSmall}; pub fn main() void { - var my_number: ??? = 5; - + var my_number: MyNumberError!usize = 5; // Looks like my_number will need to either store a number OR // an error. Can you set the type correctly above? my_number = MyNumberError.TooSmall; diff --git a/exercises/023_errors3.zig b/exercises/023_errors3.zig index 195f21a..2fdc4a2 100644 --- a/exercises/023_errors3.zig +++ b/exercises/023_errors3.zig @@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall}; pub fn main() void { const a: u32 = addTwenty(44) catch 22; - const b: u32 = addTwenty(4) ??? 22; + const b: u32 = addTwenty(4) catch 22; std.debug.print("a={}, b={}\n", .{ a, b }); } // Please provide the return type from this function. // Hint: it'll be an error union. -fn addTwenty(n: u32) ??? { +fn addTwenty(n: u32) MyNumberError!u32 { if (n < 5) { return MyNumberError.TooSmall; } else { diff --git a/exercises/024_errors4.zig b/exercises/024_errors4.zig index 02ec0f2..0115e48 100644 --- a/exercises/024_errors4.zig +++ b/exercises/024_errors4.zig @@ -53,13 +53,19 @@ fn fixTooBig(n: u32) MyNumberError!u32 { } fn fixTooSmall(n: u32) MyNumberError!u32 { + return detectProblems(n) catch |err| { + if (err == MyNumberError.TooSmall) { + return 10; + } + + return err; + }; // Oh dear, this is missing a lot! But don't worry, it's nearly // identical to fixTooBig() above. // // If we get a TooSmall error, we should return 10. // If we get any other error, we should return that error. // Otherwise, we return the u32 number. - return detectProblems(n) ???; } fn detectProblems(n: u32) MyNumberError!u32 { diff --git a/exercises/025_errors5.zig b/exercises/025_errors5.zig index 94bf1c7..6da5985 100644 --- a/exercises/025_errors5.zig +++ b/exercises/025_errors5.zig @@ -25,9 +25,7 @@ pub fn main() void { fn addFive(n: u32) MyNumberError!u32 { // This function needs to return any error which might come back from detect(). // Please use a "try" statement rather than a "catch". - // - const x = detect(n); - + const x = try detect(n); return x + 5; } diff --git a/exercises/026_hello2.zig b/exercises/026_hello2.zig index cd59b86..64c64c4 100644 --- a/exercises/026_hello2.zig +++ b/exercises/026_hello2.zig @@ -23,5 +23,5 @@ pub fn main() !void { // to be able to pass it up as a return value of main(). // // We just learned of a single statement which can accomplish this. - stdout.print("Hello world!\n", .{}); + try stdout.print("Hello world!\n", .{}); } diff --git a/exercises/027_defer.zig b/exercises/027_defer.zig index b41e2af..68d0974 100644 --- a/exercises/027_defer.zig +++ b/exercises/027_defer.zig @@ -20,6 +20,6 @@ const std = @import("std"); pub fn main() void { // Without changing anything else, please add a 'defer' statement // to this code so that our program prints "One Two\n": - std.debug.print("Two\n", .{}); + defer std.debug.print("Two\n", .{}); std.debug.print("One ", .{}); } diff --git a/exercises/028_defer2.zig b/exercises/028_defer2.zig index 35c1326..358fe28 100644 --- a/exercises/028_defer2.zig +++ b/exercises/028_defer2.zig @@ -18,7 +18,7 @@ pub fn main() void { fn printAnimal(animal: u8) void { std.debug.print("(", .{}); - std.debug.print(") ", .{}); // <---- how?! + defer std.debug.print(") ", .{}); // <---- how?! if (animal == 'g') { std.debug.print("Goat", .{}); diff --git a/exercises/029_errdefer.zig b/exercises/029_errdefer.zig index 39ab306..bda1ea2 100644 --- a/exercises/029_errdefer.zig +++ b/exercises/029_errdefer.zig @@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 { // Please make the "failed" message print ONLY if the makeNumber() // function exits with an error: - std.debug.print("failed!\n", .{}); + errdefer std.debug.print("failed!\n", .{}); var num = try getNumber(); // <-- This could fail! diff --git a/exercises/030_switch.zig b/exercises/030_switch.zig index cb983f5..b633c66 100644 --- a/exercises/030_switch.zig +++ b/exercises/030_switch.zig @@ -46,6 +46,9 @@ pub fn main() void { // match for every possible value). Please add an "else" // to this switch to print a question mark "?" when c is // not one of the existing matches. + else => { + std.debug.print("?", .{}); + }, } } diff --git a/exercises/031_switch2.zig b/exercises/031_switch2.zig index cf5b5a5..2f82e58 100644 --- a/exercises/031_switch2.zig +++ b/exercises/031_switch2.zig @@ -31,6 +31,9 @@ pub fn main() void { 26 => 'Z', // As in the last exercise, please add the 'else' clause // and this time, have it return an exclamation mark '!'. + else => { + return std.debug.print("!", .{}); + }, }; std.debug.print("{c}", .{real_char}); diff --git a/exercises/032_unreachable.zig b/exercises/032_unreachable.zig index ffc35a4..90ab452 100644 --- a/exercises/032_unreachable.zig +++ b/exercises/032_unreachable.zig @@ -35,6 +35,9 @@ pub fn main() void { 3 => { current_value *= current_value; }, + else => { + unreachable; + }, } std.debug.print("{} ", .{current_value}); diff --git a/exercises/033_iferror.zig b/exercises/033_iferror.zig index 6ba0c61..d444c57 100644 --- a/exercises/033_iferror.zig +++ b/exercises/033_iferror.zig @@ -39,6 +39,7 @@ pub fn main() void { std.debug.print("={}. ", .{value}); } else |err| switch (err) { MyNumberError.TooBig => std.debug.print(">4. ", .{}), + MyNumberError.TooSmall => std.debug.print("<4. ", .{}), // Please add a match for TooSmall here and have it print: "<4. " } } diff --git a/exercises/034_quiz4.zig b/exercises/034_quiz4.zig index 2d843f2..6241590 100644 --- a/exercises/034_quiz4.zig +++ b/exercises/034_quiz4.zig @@ -9,10 +9,16 @@ const std = @import("std"); const NumError = error{IllegalNumber}; -pub fn main() void { +pub fn main() !void { const stdout = std.io.getStdOut().writer(); - const my_num: u32 = getNumber(); + const my_num: u32 = try getNumber(); + + // if (my_num) |value| { + // stdout("ZIG! {!}", .{value}); + // } else |err| switch (err) { + // NumError.IllegalNumber => 42, + // } try stdout.print("my_num={}\n", .{my_num}); }