Updated April 20, 2023
Introduction to Golang recover()
The following article provides an outline for Golang recover(). In the go language recover function is used to handle any unexpected error, recovery is similar to try catch of other programming language, mostly we use the recover where we are not sure about the error and we do not want our program to stop execution (in case if exception occurs in mean time of execution of the program ), for example if we are trying to open any file and by mistake someone has deleted that file so in that case there will be error and hence execution of the program will stop, hence in case of handle error and display error in the proper format instead of the showing error as it is we use the recovery function.
Syntax:
In the below syntax we are showing a simple syntax for the recovery function of the go language, we see the below syntax attribute with the following steps.
- Here error is the dynamic variable you can create with any other name according to your wish.
- Recovery is a function which will handle the case of error.
- We can use the panic function to display the error in the customized format.
if error := recover(); error != nil
How recover() Function works in Go Language?
Before seeing the work of the recovery in go language we need to know why we need recovery, if you have seen any other programming language like java and dot net they have try and catch statement which deal with situation which is not in our hand, for example in case if we are performing division and by some way division become like number / 0 which means the output will be infinite and our system can go into blocking stage as they will not be able to handle infinite so for such type of situations we use the recovery.
Working of the recovery in the following steps:
- Recovery is a function which is used inside the deferred type of function, it is not meant for use in the case of any normal function.
- In case if we want to handle an error case and we are using recovery and it is not inside the deferred then it will not be able to sequences of panicking.
- Recovery function once reads the exception it will capture the exception and handle it in the correct way and inside the panic function we can display the correct out format for user understanding.
- We have to write a function inside the import of the go language and inside this function we need to write the capturing of the exception logic.
- Remember the recovery function in the go language is the inbuilt function.
Examples of Golang recover()
Given below are some of the examples which display the working of the recovery function, to execute these examples we can create a file with name recovery.go and we can execute the file after copy pasting of the below examples. We can use the command go run recovery.go and we will get the output.
Example #1
Below is an example where we are dealing with the panic condition without using defer, and you can see the output which totally looks like an error.
Code:
package main
import "fmt"
//Function to handle the recovery or simply exception cases
func exceptionCase() {
if e := recover(); e != nil {
fmt.Println("Handling the exception and exception is ", e)
}
}
func start(school *string, school_name *string) {
exceptionCase()
if school == nil {
panic("Error: The value of school can not be nil")
}
if school_name == nil {
panic("Error: The school name can not be nil")
}
fmt.Printf("The school type is: %s \n The school name is: %s\n", *school_name, *school_name)
fmt.Printf("Here we are returning response of success from the start function")
}
func main() {
school_type := "Private School"
//calling the start function which will check the passed params to it
start(&school_type, nil)
fmt.Printf("Here we are returning the response from the main function")
}
Output:
Example #2
Here we are using the recovery function along with defer and you can see the screen output, it is more understandable in nature.
Code:
package main
import "fmt"
//Function to handle the recovery or simply exception cases
func exceptionCase() {
if e := recover(); e != nil {
fmt.Println("Handling the exception and exception is ", e)
}
}
func start(student_type *string, student_name *string) {
defer exceptionCase()
if student_type == nil {
panic("Error: The value of school can not be nil")
}
if student_name == nil {
panic("Error: The school name can not be nil")
}
fmt.Printf("The Student Type: %s \n The student name: %s\n", *student_type, *student_name)
fmt.Printf("Here we are returning response of success from the start function")
}
func main() {
student_type := "Regular Student"
//Here we are starting to call for the params to start function .
start(&student_type, nil)
fmt.Printf("Here we are returning the response from the main function")
}
Output:
Example #3
In the below example we have taken some calls test1 and test2 and used the deferred, here we are using the function recovery inside the recovery and it will be used to handle panic cases.
Code:
package main
import (
"fmt"
"time"
)
//Function to handle the recovery or simply exception cases
func exceptionCase() {
if e := recover(); e != nil {
fmt.Println("Exception case is", e)
}
}
func test1() {
//Using the defer and then we are calling the exception .
defer exceptionCase()
fmt.Println("Let us greet the function test1")
go test2()
time.Sleep(11 * time.Second)
}
func test2() {
fmt.Println("Let us greet the function test2")
panic("Do not worry!!")
}
func main() {
//Calling the function test1()
test1()
fmt.Println("Here we are returning final response of main")
}
Output
Recommended Articles
This is a guide to Golang recover(). Here we discuss the introduction, syntax, and working of the recover() function in the go language along with examples and code implementation. You may also have a look at the following articles to learn more –