diff options
| author | Vincenzo Palazzo <vincenzopalazzodev@gmail.com> | 2022-05-17 21:03:42 +0200 |
|---|---|---|
| committer | Vincenzo Palazzo <vincenzopalazzodev@gmail.com> | 2022-05-17 21:08:32 +0200 |
| commit | 80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc (patch) | |
| tree | f437d0f27e6a39e715ef07d961cfa2941a151bcb /doc/langref.html.in | |
| parent | 8328f5ed51aee877dfc616be5d6fdcaa09becdca (diff) | |
| download | zig-80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc.tar.gz zig-80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc.zip | |
docs: add documentation on errdefer with caputure syntax
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Diffstat (limited to 'doc/langref.html.in')
| -rw-r--r-- | doc/langref.html.in | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index feee376e4e..839b5ed3bf 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4831,9 +4831,23 @@ fn deferErrorExample(is_error: bool) !void { } } +// 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#} @@ -11941,7 +11955,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> |
