diff options
Diffstat (limited to 'lang/zig/errdefer.zig')
-rw-r--r-- | lang/zig/errdefer.zig | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lang/zig/errdefer.zig b/lang/zig/errdefer.zig new file mode 100644 index 0000000..812b0f7 --- /dev/null +++ b/lang/zig/errdefer.zig @@ -0,0 +1,17 @@ +fn createFoo(param: i32) !Foo { + const foo = try tryToAllocateFoo(); + // now we have allocated foo. we need to free it if the function fails. + // but we want to return it if the function succeeds. + errdefer deallocateFoo(foo); + + const tmp_buf = allocateTmpBuffer() orelse return error.OutOfMemory; + // tmp_buf is truly a temporary resource, and we for sure want to clean it up + // before this block leaves scope + defer deallocateTmpBuffer(tmp_buf); + + if (param > 1337) return error.InvalidParam; + + // here the errdefer will not run since we're returning success from the function. + // but the defer will run! + return foo; +} |