Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / exp / eval / abort.go
blob22e17cec405444285aa692779060623f73dd6fff
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package eval
7 import (
8 "fmt"
9 "os"
10 "runtime"
13 // Abort aborts the thread's current computation,
14 // causing the innermost Try to return err.
15 func (t *Thread) Abort(err os.Error) {
16 if t.abort == nil {
17 panic("abort: " + err.String())
19 t.abort <- err
20 runtime.Goexit()
23 // Try executes a computation; if the computation
24 // Aborts, Try returns the error passed to abort.
25 func (t *Thread) Try(f func(t *Thread)) os.Error {
26 oc := t.abort
27 c := make(chan os.Error)
28 t.abort = c
29 go func() {
30 f(t)
31 c <- nil
32 }()
33 err := <-c
34 t.abort = oc
35 return err
38 type DivByZeroError struct{}
40 func (DivByZeroError) String() string { return "divide by zero" }
42 type NilPointerError struct{}
44 func (NilPointerError) String() string { return "nil pointer dereference" }
46 type IndexError struct {
47 Idx, Len int64
50 func (e IndexError) String() string {
51 if e.Idx < 0 {
52 return fmt.Sprintf("negative index: %d", e.Idx)
54 return fmt.Sprintf("index %d exceeds length %d", e.Idx, e.Len)
57 type SliceError struct {
58 Lo, Hi, Cap int64
61 func (e SliceError) String() string {
62 return fmt.Sprintf("slice [%d:%d]; cap %d", e.Lo, e.Hi, e.Cap)
65 type KeyError struct {
66 Key interface{}
69 func (e KeyError) String() string { return fmt.Sprintf("key '%v' not found in map", e.Key) }
71 type NegativeLengthError struct {
72 Len int64
75 func (e NegativeLengthError) String() string {
76 return fmt.Sprintf("negative length: %d", e.Len)
79 type NegativeCapacityError struct {
80 Len int64
83 func (e NegativeCapacityError) String() string {
84 return fmt.Sprintf("negative capacity: %d", e.Len)