exception

package
v0.0.0-...-90c9d3a Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2010 License: BSD-3-Clause, GooglePatentClause Imports: 2 Imported by: 0

Documentation

Overview

This package illustrates how basic try-catch exception handling can be emulated using goroutines, channels, and closures.

This package is *not* intended as a general exception handler library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exception

type Exception struct {
	Value interface{} // Value may be the nil exception
}

An Exception carries an exception value.

func Try

func Try(f func(throw Handler)) *Exception

Try invokes a function f with a Handler to throw exceptions. The function f may terminate abnormally with an arbitrary exception x by calling throw(x) within f. If an exception is thrown, Try returns an *Exception; otherwise it returns nil.

Usage pattern:

if x := exception.Try(func(throw exception.Handler) {
	...
	throw(42);  // terminate f by throwing exception 42
	...
}); x != nil {
	// catch exception, e.g. print it
	fmt.Println(x.Value);
}

Alternative:

exception.Try(func(throw exception.Handler) {
	...
	throw(42);  // terminate f by throwing exception 42
	...
}).Catch(func (x interface{}) {
	// catch exception, e.g. print it
	fmt.Println(x);
})

func (*Exception) Catch

func (x *Exception) Catch(f Handler)

If x != nil, Catch invokes f with the exception value x.Value. See Try for usage patterns.

func (*Exception) String

func (x *Exception) String() string

type Handler

type Handler func(x interface{})

A Handler function handles an arbitrary exception value x.

Jump to

Keyboard shortcuts

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