Documentation ¶
Overview ¶
Handle runtime error.
Example ¶
package main import ( "fmt" "log" "github.com/suzuki-shunsuke/rterror" ) func hello() (msg string, e error) { // declare named return value defer func() { p := recover() if p != nil { // assign RuntimeError to the surrounding function's return value e = rterror.NewRuntimeError(p, "") } }() var s *string fmt.Println(*s) // panic return "hello", nil } func main() { msg, err := hello() if err != nil { log.Fatal(err) } fmt.Println(msg) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RuntimeError ¶
type RuntimeError interface { Param() interface{} Error() string }
Runtime Error interface
Example ¶
msg, err := func() (msg string, e error) { // declare named return value defer func() { p := recover() if p != nil { // assign RuntimeError to the surrounding function's return value e = rterror.NewRuntimeError(p, "") } }() var s *string fmt.Println(*s) // panic return "hello", nil }() if err != nil { log.Fatal(err) } fmt.Println(msg)
Output:
func NewRuntimeError ¶
func NewRuntimeError(p interface{}, msg string) RuntimeError
RuntimeError's constructor. `p` is a value given to panic. `msg` is a return value of Error(). If `msg` is a empty string ("") and `p` holds a `string`, Error() returns the underlying value. If `msg` is a empty string ("") but `p` doesn't hold a string, `Error()` returns a string "Runtime Error".
Click to show internal directories.
Click to hide internal directories.