| ... | @@ -4831,9 +4831,23 @@ fn deferErrorExample(is_error: bool) !void { | ... | @@ -4831,9 +4831,23 @@ fn deferErrorExample(is_error: bool) !void { |
| 4831 | } | 4831 | } |
| 4832 | } | 4832 | } |
| 4833 | | 4833 | |
| | 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. |
| | 4839 | fn deferErrorCaptureExample() !void { |
| | 4840 | errdefer |err| { |
| | 4841 | std.debug.print("the error is {s}\n", .{@errorName(err)}); |
| | 4842 | } |
| | 4843 | |
| | 4844 | return error.DeferError; |
| | 4845 | } |
| | 4846 | |
| 4834 | test "errdefer unwinding" { | 4847 | test "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> |