# Zig-recover - Regain control when a function panics

**URL:** <https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147>\
**Category:** Showcase\
**Tags:** error-handling\
**Created:** [February 5, 2024, 3:53pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147 "2024-02-05T15:53:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [February 5, 2024, 3:53pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147/1 "2024-02-05T15:53:32Z")

</div>

> **[GitHub - dimdin/zig-recover: zig panic recover](https://github.com/dimdin/zig-recover)**
>
> zig panic recover. Contribute to dimdin/zig-recover development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Calder-Ty](https://ziggit.dev/user_avatar/ziggit.dev/calder-ty/32/10237_2.png) [@Calder-Ty](https://ziggit.dev/u/Calder-Ty)\
**Post date:** [February 5, 2024, 4:34pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147/2 "2024-02-05T16:34:19Z")

</div>

> Proper use of recover is only for testing panic and undefined behavior runtime safety checks.

Do you have an example of how you do this?

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [February 5, 2024, 4:54pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147/3 "2024-02-05T16:54:07Z")

</div>

Let’s say that `foo(0)` is designed to panic.

To test that the function panics you can:

```zig
test "foo(0) panics" {
     const err = recover.call(foo, .{0}, error.Panic);
     std.testing.expectError(error.Panic, err).
}

```

For this to work, you need a custom test\_runner with a panic handler:

```zig
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
    recover.panicked();
    std.builtin.default_panic(msg, error_return_trace, ret_addr);
}

```

---

<div class="post-metadata">

**Author:** ![Calder-Ty](https://ziggit.dev/user_avatar/ziggit.dev/calder-ty/32/10237_2.png) [@Calder-Ty](https://ziggit.dev/u/Calder-Ty)\
**Post date:** [February 5, 2024, 5:55pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147/4 "2024-02-05T17:55:09Z")

</div>

I see. Thanks, that’s a nice use case.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 29, 2024, 9:07pm UTC](https://ziggit.dev/t/zig-recover-regain-control-when-a-function-panics/3147/5 "2024-08-29T21:07:50Z")

</div>

zig-recover version 1.0.0 released for zig 0.12.0 or later

To fetch as a dependency in your `build.zig.zon`:

```shell
zig fetch --save https://github.com/dimdin/zig-recover/archive/refs/tags/1.0.0.tar.gz

```

Usage is simplified:

```zig
const recover = @import("recover");
pub const panic = recover.panic;

fn function() void {
    unreachable;
}

pub fn main() !void {
    recover.call(function, .{}) catch {};
    // flow continues here because recover.call recovers
    // from unreachable and returns error.Panic
}

```

See the project [README.md](https://github.com/dimdin/zig-recover/blob/1.0.0/README.md) for more information.
