exceptions

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 2 Imported by: 7 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

exceptions

Simple exceptions support for Go, using panic() and recover().

Go's panic(any) will unwind the stack one frame at a time until there is a matching call to recover(). This library calls the parameter passed to panic an exception, and provides simplified methods to catch those exceptions.

Examples

  • Catching an exception (any panic):
    import . "github.com/gomlx/exceptions"

    ...
	exception := Try(fn)
    if exception != nil {
        ... 
    }
  • Throwing an exception: one can simply panic or use the provided Panicf, which converts the given text into an error+stack:
    func MyRatio(myThing, total float64) float64 {
		if total == 0 {
			Panicf("MyRatio(%f, %f) has no things (0) to calculate a ratio from", myThing, total)
        }
		return myThing/total
    }
  • Go panic supports any value to be passed, which we interpret as an exception. TryCatch[E any] allows a function to be called and an exception of type E to be caught. Any other exceptions are not handled and continues to panic.
	var x ResultType
	err := TryCatch[error](func() { x = DoSomething() })
	if err != nil {
		// Handle error ...
	}

Documentation

Overview

Package exceptions provides helper functions to leverage Go's `panic`, `recover` and `defer` as an "exceptions" system.

The `panic`, `recover` and the added runtime type checking is slow when compared to simply returning errors. So it should be used where a little latency in case of errors is not an issue.

It defines `Try` and `TryCatch[E any]`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Panicf

func Panicf(format string, args ...any)

Panicf is a shortcut to `panic(errors.Errorf(format, args...))`. It throws an error with a stack-trace and the formatted message.

func Try

func Try(fn func()) (exception any)

Try calls `fn` and return any exception (`panic`) that may occur.

Runtime panics are converted to an error with a stack-trace, to facilitate debugging.

Example:

var x ResultType
e := Try(func() { x = DoSomething() })
if e != nil {
	if eInt, ok := e.(int); ok {
		errorInt = e
	} else if err, ok := e.(err); ok {
		klog.Errorf("%v", e)
	} else {
		panic(e)
	}
}

func TryCatch

func TryCatch[E any](fn func()) (exception E)

TryCatch executes `fn` and in case of `panic`, it recovers if of the type `E`. For a `panic` of any other type, it simply re-throws the `panic`.

Runtime panics are converted to errors.Error, with a stack trace.

Example:

var x ResultType
err := TryCatch[error](func() { x = DoSomething() })
if err != nil {
	// Handle error ...
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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