Finished quiz 4 - dealing with errors
This commit is contained in:
parent
af15995fc7
commit
d12d5a677c
@ -9,7 +9,7 @@
|
|||||||
// "TooSmall". Please add it where needed!
|
// "TooSmall". Please add it where needed!
|
||||||
const MyNumberError = error{
|
const MyNumberError = error{
|
||||||
TooBig,
|
TooBig,
|
||||||
???,
|
TooSmall,
|
||||||
TooFour,
|
TooFour,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ pub fn main() void {
|
|||||||
if (number_error == MyNumberError.TooBig) {
|
if (number_error == MyNumberError.TooBig) {
|
||||||
std.debug.print(">4. ", .{});
|
std.debug.print(">4. ", .{});
|
||||||
}
|
}
|
||||||
if (???) {
|
if (number_error == MyNumberError.TooSmall) {
|
||||||
std.debug.print("<4. ", .{});
|
std.debug.print("<4. ", .{});
|
||||||
}
|
}
|
||||||
if (number_error == MyNumberError.TooFour) {
|
if (number_error == MyNumberError.TooFour) {
|
||||||
|
|||||||
@ -19,8 +19,7 @@ const std = @import("std");
|
|||||||
const MyNumberError = error{TooSmall};
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
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
|
// Looks like my_number will need to either store a number OR
|
||||||
// an error. Can you set the type correctly above?
|
// an error. Can you set the type correctly above?
|
||||||
my_number = MyNumberError.TooSmall;
|
my_number = MyNumberError.TooSmall;
|
||||||
|
|||||||
@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall};
|
|||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const a: u32 = addTwenty(44) catch 22;
|
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 });
|
std.debug.print("a={}, b={}\n", .{ a, b });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Please provide the return type from this function.
|
// Please provide the return type from this function.
|
||||||
// Hint: it'll be an error union.
|
// Hint: it'll be an error union.
|
||||||
fn addTwenty(n: u32) ??? {
|
fn addTwenty(n: u32) MyNumberError!u32 {
|
||||||
if (n < 5) {
|
if (n < 5) {
|
||||||
return MyNumberError.TooSmall;
|
return MyNumberError.TooSmall;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -53,13 +53,19 @@ fn fixTooBig(n: u32) MyNumberError!u32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fixTooSmall(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
|
// Oh dear, this is missing a lot! But don't worry, it's nearly
|
||||||
// identical to fixTooBig() above.
|
// identical to fixTooBig() above.
|
||||||
//
|
//
|
||||||
// If we get a TooSmall error, we should return 10.
|
// If we get a TooSmall error, we should return 10.
|
||||||
// If we get any other error, we should return that error.
|
// If we get any other error, we should return that error.
|
||||||
// Otherwise, we return the u32 number.
|
// Otherwise, we return the u32 number.
|
||||||
return detectProblems(n) ???;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detectProblems(n: u32) MyNumberError!u32 {
|
fn detectProblems(n: u32) MyNumberError!u32 {
|
||||||
|
|||||||
@ -25,9 +25,7 @@ pub fn main() void {
|
|||||||
fn addFive(n: u32) MyNumberError!u32 {
|
fn addFive(n: u32) MyNumberError!u32 {
|
||||||
// This function needs to return any error which might come back from detect().
|
// This function needs to return any error which might come back from detect().
|
||||||
// Please use a "try" statement rather than a "catch".
|
// Please use a "try" statement rather than a "catch".
|
||||||
//
|
const x = try detect(n);
|
||||||
const x = detect(n);
|
|
||||||
|
|
||||||
return x + 5;
|
return x + 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,5 +23,5 @@ pub fn main() !void {
|
|||||||
// to be able to pass it up as a return value of main().
|
// to be able to pass it up as a return value of main().
|
||||||
//
|
//
|
||||||
// We just learned of a single statement which can accomplish this.
|
// We just learned of a single statement which can accomplish this.
|
||||||
stdout.print("Hello world!\n", .{});
|
try stdout.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,6 +20,6 @@ const std = @import("std");
|
|||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
// Without changing anything else, please add a 'defer' statement
|
// Without changing anything else, please add a 'defer' statement
|
||||||
// to this code so that our program prints "One Two\n":
|
// 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 ", .{});
|
std.debug.print("One ", .{});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ pub fn main() void {
|
|||||||
fn printAnimal(animal: u8) void {
|
fn printAnimal(animal: u8) void {
|
||||||
std.debug.print("(", .{});
|
std.debug.print("(", .{});
|
||||||
|
|
||||||
std.debug.print(") ", .{}); // <---- how?!
|
defer std.debug.print(") ", .{}); // <---- how?!
|
||||||
|
|
||||||
if (animal == 'g') {
|
if (animal == 'g') {
|
||||||
std.debug.print("Goat", .{});
|
std.debug.print("Goat", .{});
|
||||||
|
|||||||
@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 {
|
|||||||
|
|
||||||
// Please make the "failed" message print ONLY if the makeNumber()
|
// Please make the "failed" message print ONLY if the makeNumber()
|
||||||
// function exits with an error:
|
// function exits with an error:
|
||||||
std.debug.print("failed!\n", .{});
|
errdefer std.debug.print("failed!\n", .{});
|
||||||
|
|
||||||
var num = try getNumber(); // <-- This could fail!
|
var num = try getNumber(); // <-- This could fail!
|
||||||
|
|
||||||
|
|||||||
@ -46,6 +46,9 @@ pub fn main() void {
|
|||||||
// match for every possible value). Please add an "else"
|
// match for every possible value). Please add an "else"
|
||||||
// to this switch to print a question mark "?" when c is
|
// to this switch to print a question mark "?" when c is
|
||||||
// not one of the existing matches.
|
// not one of the existing matches.
|
||||||
|
else => {
|
||||||
|
std.debug.print("?", .{});
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,6 +31,9 @@ pub fn main() void {
|
|||||||
26 => 'Z',
|
26 => 'Z',
|
||||||
// As in the last exercise, please add the 'else' clause
|
// As in the last exercise, please add the 'else' clause
|
||||||
// and this time, have it return an exclamation mark '!'.
|
// and this time, have it return an exclamation mark '!'.
|
||||||
|
else => {
|
||||||
|
return std.debug.print("!", .{});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
std.debug.print("{c}", .{real_char});
|
std.debug.print("{c}", .{real_char});
|
||||||
|
|||||||
@ -35,6 +35,9 @@ pub fn main() void {
|
|||||||
3 => {
|
3 => {
|
||||||
current_value *= current_value;
|
current_value *= current_value;
|
||||||
},
|
},
|
||||||
|
else => {
|
||||||
|
unreachable;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("{} ", .{current_value});
|
std.debug.print("{} ", .{current_value});
|
||||||
|
|||||||
@ -39,6 +39,7 @@ pub fn main() void {
|
|||||||
std.debug.print("={}. ", .{value});
|
std.debug.print("={}. ", .{value});
|
||||||
} else |err| switch (err) {
|
} else |err| switch (err) {
|
||||||
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
|
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
|
||||||
|
MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
|
||||||
// Please add a match for TooSmall here and have it print: "<4. "
|
// Please add a match for TooSmall here and have it print: "<4. "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,10 +9,16 @@ const std = @import("std");
|
|||||||
|
|
||||||
const NumError = error{IllegalNumber};
|
const NumError = error{IllegalNumber};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() !void {
|
||||||
const stdout = std.io.getStdOut().writer();
|
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});
|
try stdout.print("my_num={}\n", .{my_num});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user