Recover calls a function and regains control of the calling thread when the function panics or behaves undefined. Recover is licensed under the terms of the MIT License.
Recover call, calls function with args, if the function does not panic,
will return the called function's return value. If the function panics, will
return error.Panic.
const recover = @import("recover");
try recover.call(function, args);
-
Enabled runtime safety checks, such as unreachable, index out of bounds, overflow, division by zero, incorrect pointer alignment, etc.
-
In the root source file define panic as recover.panic or override the default panic handler and call recover
panicked.
pub const panic = recover.panic;
- Excluding Windows, linking to C standard library is required.
Returns error.Panic because function division panics with runtime error
"division by zero".
fn division(num: u32, den: u32) u32 {
return num / den;
}
try recover.call(division, .{1, 0});
For recover to work for testing, you need a custom test runner with a panic handler:
pub const panic = @import("recover").panic;
To test that foo(0) panics:
test "foo(0) panics" {
const err = recover.call(foo, .{0});
std.testing.expectError(error.Panic, err).
}
- Recover is useful for testing panic and undefined behavior runtime safety checks.
- It is not recommended to use recover as a general exception mechanism.