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