English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statement

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go Language Recover (Recovery)

Recover is used to regain control of the program from emergency or error situations. It stops the termination sequence and returns to normal execution. It is called from a deferred function. It retrieves the error value passed through the panic call. It usually returnsnilwith no other effect.

Go recover() Example

package main
import (
   "fmt"
)
func main() {
   fmt.Println(SaveDivide(10, 0))
   fmt.Println(SaveDivide(10, 10))
}
func SaveDivide(num1, num2 int) int {
   defer func() {
      fmt.Println(recover())
   }()
   quotient := num1 / num2
   return quotient
}

Output:

runtime error: integer divide by zero
0
<nil>
1