2021-01-31 17:48:34 -05:00
|
|
|
//
|
|
|
|
// Another common problem is a block of code that could exit in multiple
|
2021-03-26 18:21:53 -04:00
|
|
|
// places due to an error - but that needs to do something before it
|
2021-01-31 17:48:34 -05:00
|
|
|
// exits (typically to clean up after itself).
|
|
|
|
//
|
|
|
|
// An "errdefer" is a defer that only runs if the block exits with an error:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// errdefer cleanup();
|
|
|
|
// try canFail();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The cleanup() function is called ONLY if the "try" statement returns an
|
|
|
|
// error produced by canFail().
|
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
var counter: u32 = 0;
|
|
|
|
|
|
|
|
const MyErr = error{ GetFail, IncFail };
|
|
|
|
|
|
|
|
pub fn main() void {
|
|
|
|
// We simply quit the entire program if we fail to get a number:
|
2023-11-21 09:01:22 -05:00
|
|
|
const a: u32 = makeNumber() catch return;
|
|
|
|
const b: u32 = makeNumber() catch return;
|
2021-01-31 17:48:34 -05:00
|
|
|
|
2021-02-15 16:55:44 -05:00
|
|
|
std.debug.print("Numbers: {}, {}\n", .{ a, b });
|
|
|
|
}
|
2021-01-31 17:48:34 -05:00
|
|
|
|
|
|
|
fn makeNumber() MyErr!u32 {
|
|
|
|
std.debug.print("Getting number...", .{});
|
|
|
|
|
|
|
|
// Please make the "failed" message print ONLY if the makeNumber()
|
|
|
|
// function exits with an error:
|
|
|
|
std.debug.print("failed!\n", .{});
|
|
|
|
|
2021-02-15 16:55:44 -05:00
|
|
|
var num = try getNumber(); // <-- This could fail!
|
2021-01-31 17:48:34 -05:00
|
|
|
|
|
|
|
num = try increaseNumber(num); // <-- This could ALSO fail!
|
|
|
|
|
|
|
|
std.debug.print("got {}. ", .{num});
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getNumber() MyErr!u32 {
|
|
|
|
// I _could_ fail...but I don't!
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increaseNumber(n: u32) MyErr!u32 {
|
|
|
|
// I fail after the first time you run me!
|
|
|
|
if (counter > 0) return MyErr.IncFail;
|
2021-02-15 16:55:44 -05:00
|
|
|
|
2021-01-31 17:48:34 -05:00
|
|
|
// Sneaky, weird global stuff.
|
|
|
|
counter += 1;
|
|
|
|
|
|
|
|
return n + 1;
|
|
|
|
}
|