PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pkg/gob/error.go

https://bitbucket.org/taruti/go.plan9
Go | 41 lines | 20 code | 7 blank | 14 comment | 2 complexity | 38024ee24462676b64c02bea4ca225c9 MD5 | raw file
Possible License(s): BSD-3-Clause
  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.
  4. package gob
  5. import (
  6. "fmt"
  7. "os"
  8. )
  9. // Errors in decoding and encoding are handled using panic and recover.
  10. // Panics caused by user error (that is, everything except run-time panics
  11. // such as "index out of bounds" errors) do not leave the file that caused
  12. // them, but are instead turned into plain os.Error returns. Encoding and
  13. // decoding functions and methods that do not return an os.Error either use
  14. // panic to report an error or are guaranteed error-free.
  15. // A gobError wraps an os.Error and is used to distinguish errors (panics) generated in this package.
  16. type gobError struct {
  17. os.Error
  18. }
  19. // errorf is like error but takes Printf-style arguments to construct an os.Error.
  20. func errorf(format string, args ...interface{}) {
  21. error(fmt.Errorf(format, args...))
  22. }
  23. // error wraps the argument error and uses it as the argument to panic.
  24. func error(err os.Error) {
  25. panic(gobError{Error: err})
  26. }
  27. // catchError is meant to be used as a deferred function to turn a panic(gobError) into a
  28. // plain os.Error. It overwrites the error return of the function that deferred its call.
  29. func catchError(err *os.Error) {
  30. if e := recover(); e != nil {
  31. *err = e.(gobError).Error // Will re-panic if not one of our errors, such as a runtime error.
  32. }
  33. return
  34. }