authorgravatar for vincenzopalazzodev@gmail.comVincenzo Palazzo <vincenzopalazzodev@gmail.com> 2022-05-17 21:03:42+02:00
committergravatar for vincenzopalazzodev@gmail.comVincenzo Palazzo <vincenzopalazzodev@gmail.com> 2022-05-17 21:08:32+02:00
log80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc
treef437d0f27e6a39e715ef07d961cfa2941a151bcb
parent8328f5ed51aee877dfc616be5d6fdcaa09becdca
signaturelock-open Commit is signed but in an unrecognized format.

docs: add documentation on errdefer with caputure syntax

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

1 files changed, 15 insertions(+), 1 deletions(-)

doc/langref.html.in+15-1
...@@ -4831,9 +4831,23 @@ fn deferErrorExample(is_error: bool) !void {...@@ -4831,9 +4831,23 @@ fn deferErrorExample(is_error: bool) !void {
4831 }4831 }
4832}4832}
48334833
4834// The errdefer keyword support also an alternative syntax to capture the
4835// error generated in case of one error.
4836//
4837// This is useful when during the clean up after an error additional
4838// message want to be printed.
4839fn deferErrorCaptureExample() !void {
4840 errdefer |err| {
4841 std.debug.print("the error is {s}\n", .{@errorName(err)});
4842 }
4843
4844 return error.DeferError;
4845}
4846
4834test "errdefer unwinding" {4847test "errdefer unwinding" {
4835 deferErrorExample(false) catch {};4848 deferErrorExample(false) catch {};
4836 deferErrorExample(true) catch {};4849 deferErrorExample(true) catch {};
4850 deferErrorCaptureExample() catch {};
4837}4851}
4838 {#code_end#}4852 {#code_end#}
4839 {#see_also|Errors#}4853 {#see_also|Errors#}
...@@ -11941,7 +11955,7 @@ fn readU32Be() u32 {}...@@ -11941,7 +11955,7 @@ fn readU32Be() u32 {}
11941 <pre>{#syntax#}errdefer{#endsyntax#}</pre>11955 <pre>{#syntax#}errdefer{#endsyntax#}</pre>
11942 </th>11956 </th>
11943 <td>11957 <td>
11944 {#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error.11958 {#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.
11945 <ul>11959 <ul>
11946 <li>See also {#link|errdefer#}</li>11960 <li>See also {#link|errdefer#}</li>
11947 </ul>11961 </ul>