blob: e34d0f0d765bf0a136987bda6374eb960500816f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// 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.
//
package exception
import (
"fmt"
"runtime"
)
// A Handler function handles an arbitrary exception value x.
type Handler func(x interface{})
// An Exception carries an exception value.
type Exception struct {
Value interface{} // Value may be the nil 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 Try(f func(throw Handler)) *Exception {
h := make(chan *Exception)
// execute try block
go func() {
f(func(x interface{}) {
h <- &Exception{x}
runtime.Goexit()
})
h <- nil // clean termination
}()
return <-h
}
// If x != nil, Catch invokes f with the exception value x.Value.
// See Try for usage patterns.
func (x *Exception) Catch(f Handler) {
if x != nil {
f(x.Value)
}
}
func (x *Exception) String() string {
if x != nil {
return fmt.Sprintf("exception: %v", x.Value)
}
return ""
}
|