throwable.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Package exception defines and implements a Throwable interface
  2. // with stacktrace information.
  3. //
  4. // Throwable is not meant to replace error in Go.
  5. // Throwable is meant to represent unchecked exceptions.
  6. //
  7. // Exception
  8. //
  9. // The Exception struct implements Throwable interface.
  10. // New exceptions can be defined by embedding Exception,
  11. // if customizing methods of Throwable is not required.
  12. //
  13. // For example, a NullReference exception can be defined as following:
  14. //
  15. // type NullReference struct {
  16. // *exception.Exception
  17. // }
  18. // func NewNullReference(message string, cause Throwable) *NullReference {
  19. // return &NullReference{exception.New(message, cause)}
  20. // }
  21. //
  22. // NullReference
  23. //
  24. // The above NullReference exception is included in this package.
  25. //
  26. package exception
  27. import (
  28. "io"
  29. )
  30. // Throwable is like an error, but with stacktrace information.
  31. type Throwable interface {
  32. // Throwable returns the cause of this throwable
  33. // or nil if the cause is nonexistent or unknown.
  34. Cause() Throwable
  35. // Message returns the detail message string of this throwable.
  36. Message() string
  37. // StackTrace provides the stack trace information.
  38. StackTrace() []uintptr
  39. // EliminatedStackTrace is like StackTrace but with recursive calls eliminated.
  40. // It does not eliminates cross recursions like `f(g(f(g(f()))))`.
  41. EliminatedStackTrace() []uintptr
  42. // SetStackTrace sets the stack trace information.
  43. SetStackTrace([]uintptr)
  44. // PrintStackTrace prints this throwable and its backtrace
  45. // to the specified print writer.
  46. PrintStackTrace(writer io.Writer)
  47. // Error returns a string representation of this throwable,
  48. // which is used by functions like fmt.Print and fmt.Printf("`%v`").
  49. // Any type satisfies Throwable also satisfies error.
  50. Error() string
  51. }