File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -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>
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments