exn

package
v3.1.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: MIT, Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package exn provides an exception-like mechanism.

It is a bit easier to reason about than standard exceptions, since not any code can throw, only code given access to a throw callback. See the docs for Try.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Try

func Try[T any](f func(Thrower) T) (result T, err error)

Try invokes f, which is a callback with type:

func(throw Thrower) T

Thrower is an alias for func(error, ...string).

If f returns normally, Try returns the value f returned and a nil error.

If f invokes throw with a non-nil error, f's execution is terminated, and Try returns the zero value for T and the error that was passed to throw.

If f invokes throw with a nil argument, throw returns normally without terminating f. This is so that error handling code can pass errors to throw unconditionally, like:

v, err := foo()
throw(err)

f must not store throw or otherwise cause it to be invoked after Try returns.

Callers may pass additonal string arguments to throw, which will wrap the error with additional context, i.e. throw(err, "something") is equivalent to throw(Wrap("something", err))

func Try0

func Try0(f func(Thrower)) error

Try0 is like Try, but f does not return a value, and Try0 only returns an error.

func Try2

func Try2[A, B any](f func(Thrower) (A, B)) (A, B, error)

Try2 is like Try, but with two values instead of 1.

func Wrap

func Wrap(context string, err error) error

Wrap wraps err, adding the string context to its message. If err is nil, returns nil instead.

func WrapThrow

func WrapThrow(th Thrower, context string, err error)

WrapThrow(th, context, err) is equivalent to th(Wrap(context, err)), i.e. if err != nil, it throws with an error wrapping err and supplying the additional context.

Types

type Thrower

type Thrower = func(error, ...string)

An alias for the type of the callback provided by Try.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL