aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-09-14 19:26:28 -0400
committerGitHub <noreply@github.com>2022-09-14 19:26:28 -0400
commit1d041d3a10ec9187b7c4de6b4eb8a90926ea8d6c (patch)
tree9f08adb741ae1bd98baf806ac6ab3f099cb304bf /doc
parent0931dda9a95e14b97a84e60aed424fd8bb5e1232 (diff)
parent80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc (diff)
downloadzig-1d041d3a10ec9187b7c4de6b4eb8a90926ea8d6c.tar.gz
zig-1d041d3a10ec9187b7c4de6b4eb8a90926ea8d6c.zip
Merge pull request #11664 from vincenzopalazzo/macros/docs
docs: add missed docs for some language feature like `defer` and `errdefer`
Diffstat (limited to 'doc')
-rw-r--r--doc/langref.html.in29
1 files changed, 28 insertions, 1 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index cbd841e790..02e1dc4273 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -4862,14 +4862,41 @@ fn deferErrorExample(is_error: bool) !void {
print("encountered an error!\n", .{});
}
+ // inside a defer method the return statement
+ // is not allowed.
+ // The following lines produce the following
+ // error if uncomment
+ // ```
+ // defer.zig:73:9: error: cannot return from defer expression
+ // return error.DeferError;
+ // ```
+ //
+ //defer {
+ // return error.DeferError;
+ //}
+
if (is_error) {
return error.DeferError;
}
}
+// The errdefer keyword support also an alternative syntax to capture the
+// error generated in case of one error.
+//
+// This is useful when during the clean up after an error additional
+// message want to be printed.
+fn deferErrorCaptureExample() !void {
+ errdefer |err| {
+ std.debug.print("the error is {s}\n", .{@errorName(err)});
+ }
+
+ return error.DeferError;
+}
+
test "errdefer unwinding" {
deferErrorExample(false) catch {};
deferErrorExample(true) catch {};
+ deferErrorCaptureExample() catch {};
}
{#code_end#}
{#see_also|Errors#}
@@ -11930,7 +11957,7 @@ fn readU32Be() u32 {}
<pre>{#syntax#}errdefer{#endsyntax#}</pre>
</th>
<td>
- {#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error.
+ {#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error, the errdefer expression can capture the unwrapped value.
<ul>
<li>See also {#link|errdefer#}</li>
</ul>