ADVERTISEMENT
ADVERTISEMENT

Golang recover() Function with Examples

Golang | recover() Function: Here, we are going to learn about the built-in recover() function with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 21, 2021

recover() Function

In the Go programming language, the recover() is a built-in function that is used to manage the behavior of a panicking goroutine. The recover() function is called inside a deferred function (but not any function called by it) to stop the panicking sequence by restoring normal execution and retrieves the error value passed to the call of panic. If the recover() function is called outside the deferred function it will not stop a panicking sequence. In this case, or when the goroutine is not panicking, or if the argument supplied to panic was nil, recover returns nil. Thus, the return value from recover reports whether the goroutine is panicking. (Reference: recover())

It accepts nothing and returns an interface.

Syntax:

func recover() interface{}

Parameter(s):

  • None

Return Value:

The return type of the recover() function is an interface, it returns the text, value or any customize type i.e., interface.

Example 1:

// Golang program to demonstrate the
// example of recover() function

package main

import (
	"fmt"
)

func HandleDivideByZero() {
	if r := recover(); r != nil {
		fmt.Println("Recovering from panic:", r)
	}
}

// Creating the function to return the
// result of divide (/) operation,
// and returns an error using the
// panic() function, if the dividend is 0
// And, Recovering from the panic() using
// recover() function
func Divide(a, b int) float32 {
	if b != 0 {
		return float32(a) / float32(b)
	} else {
		defer HandleDivideByZero()
		panic("Error: dividend cannot be 0.")
	}
}

// Main Function
func main() {
	x := 10
	y := 3

	res := Divide(x, y)
	fmt.Println("Result:", res)

	x = 10
	y = 0

	res = Divide(x, y)
	fmt.Println("Result:", res)
}

Output:

Result: 3.3333333
Recovering from panic: Error: dividend cannot be 0.
Result: 0

Example 2:

// Golang program to demonstrate the
// example of recover() function

package main

import (
	"fmt"
)

// Defining a struct type
type student struct {
	Name   string
	Course string
	Age    int
}

func HandleEmpty() {
	if r := recover(); r != nil {
		fmt.Println("Recovering from panic:", r)
	}
}

// Function to print the values
// and returns an error using the
// panic() function if course's value
// is nil
// And, Recovering from the panic() using
// recover() function
func PrintStudent(x student) {
	if x.Course == "" {
		defer HandleEmpty()
		panic("Course cannot be empty.")
	} else {
		fmt.Printf("%s, %s, %d\n", x.Name, x.Course, x.Age)
	}
}

// Main Function
func main() {
	// Creating structure
	s1 := student{"Radib Kar", "B.Tech", 21}
	s2 := student{"Shivang Yadav", "BCA", 22}
	s3 := student{"Pooja Kumari", "", 28}

	// Printing the values
	PrintStudent(s1)
	PrintStudent(s2)
	PrintStudent(s3)
}

Output:

Radib Kar, B.Tech, 21
Shivang Yadav, BCA, 22
Recovering from panic: Course cannot be empty.

Golang builtin Package »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.