> package main
>
> import "fmt"
>
> func x() (int, error) {
> return 0, fmt.Errorf("Error message.")
> }
>
>
> func y() int {
> panic("Panic message.")
> return 0
> }
>
> // Of course panic_if_err and err_if_pan keywords should be something
> shorter, more sensible.
> func main() {
> // We could panic with the last error return value.
> i := panic_if_err x()
> // Or reverse
> j, err := err_if_pan y()
> }
>
panic isn't like throw exception, while at first view both do the same
thing (blow everything to stop the current flow of execution),
in Go panic should be used only in situatios of "panic" :-)
Every program should be able to recover from a error situation with
some control, just using panic_if_err don't do that. In this case, the
problem is just passed up in the stack until the process is stop or to
a defer statement, where you can recover from the error.
Also, the err_if_pan can be implemented using defer/recover
func A() (err error) {
defer func() { e := recover(); if e != nil { err =
errors.New(fmt.Sprintf("err: %v", e)) }
panic("ouch!")
}
}
--
André Moraes
http://amoraes.info