| author | |
| committer | |
| log | 82679297422d02039c992cc223f5cfbf6fcd8675 |
| tree | 4e6a1a0204638b4cc67ae872731ad1a45c02848d |
| parent | aa73bb6bc969ef053a8fab265da8bf28e024b2d2 |
2 files changed, 23 insertions(+), 0 deletions(-)
doc/langref.html.in+4| ... | ... | @@ -2992,6 +2992,10 @@ fn createFoo(param: i32) !Foo { |
| 2992 | 2992 | the verbosity and cognitive overhead of trying to make sure every exit path |
| 2993 | 2993 | is covered. The deallocation code is always directly following the allocation code. |
| 2994 | 2994 | </p> |
| 2995 | <p> | |
| 2996 | The {#syntax#}errdefer{#endsyntax#} statement can optionally capture the error: | |
| 2997 | </p> | |
| 2998 | {#code|test_errdefer_capture.zig#} | |
| 2995 | 2999 | {#header_close#} |
| 2996 | 3000 | {#header_open|Common errdefer Slip-Ups#} |
| 2997 | 3001 | <p> |
doc/langref/test_errdefer_capture.zig created+19| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | fn captureError(captured: *?anyerror) !void { | |
| 4 | errdefer |err| { | |
| 5 | captured.* = err; | |
| 6 | } | |
| 7 | return error.GeneralFailure; | |
| 8 | } | |
| 9 | ||
| 10 | test "errdefer capture" { | |
| 11 | var captured: ?anyerror = null; | |
| 12 | ||
| 13 | if (captureError(&captured)) unreachable else |err| { | |
| 14 | try std.testing.expectEqual(error.GeneralFailure, captured.?); | |
| 15 | try std.testing.expectEqual(error.GeneralFailure, err); | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | // test |