Exceptions

Panic

Panic is a built-in function that stops the normal execution flow. When you call panic in your code, it means you’ve decided that your caller can’t solve the problem. Therefore, you should use panic only in rare cases where it’s not safe for your code or anyone integrating your code to continue at that point. The code sample below demonstrates how panic works:

import ( "errors" ) func A() { defer println("Then we can't save the earth!") B() } func B() { defer println("And if it keeps getting hotter...") C() } func C() { defer println("Turn on the air conditioner...") Break() } func Break() { defer println("If it's more than 30 degrees...") panic(errors.New("Global Warming!!!")) println("Goodbye!") } A()

As shown above, when panic is used and not handled, the execution flow stops, all deferred functions are executed in reverse order, and stack traces are printed.

Recover

To report an error as a return value, you have to call the recover function in the same goroutine as where the panic function is called, retrieve an error struct from the recover function, and pass it to a variable:

import ( "errors" ) func saveEarth() (err error) { defer func() { if r := recover(); r != nil { err = r.(error) } }() TooLate() return } func TooLate() { A() panic(errors.New("Then there's nothing we can do")) } func A() { defer println("If it's more than 100 degrees...") } err := saveEarth() println(err)

Every deferred function will be executed after a function call but before a return statement. Therefore, you can set a returned variable before a return statement is executed.

Next example: Methods