Handling Panics with Golang Recover

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Golang, recover is a built-in function that can be used to recover from a panic. This lab will test your ability to use recover to handle panics.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/ErrorHandlingGroup(["`Error Handling`"]) go/ErrorHandlingGroup -.-> go/recover("`Recover`") subgraph Lab Skills go/recover -.-> lab-15500{{"`Handling Panics with Golang Recover`"}} end

Recover

The mayPanic function in the provided code will panic when called. Your task is to modify the main function to recover from the panic and print the error message.

  • Use the recover function to handle the panic in the mayPanic function.
  • Print the error message when a panic occurs.
$ go run recover.go
Recovered. Error:
a problem

There is the full code below:

// Go makes it possible to _recover_ from a panic, by
// using the `recover` built-in function. A `recover` can
// stop a `panic` from aborting the program and let it
// continue with execution instead.

// An example of where this can be useful: a server
// wouldn't want to crash if one of the client connections
// exhibits a critical error. Instead, the server would
// want to close that connection and continue serving
// other clients. In fact, this is what Go's `net/http`
// does by default for HTTP servers.

package main

import "fmt"

// This function panics.
func mayPanic() {
	panic("a problem")
}

func main() {
	// `recover` must be called within a deferred function.
	// When the enclosing function panics, the defer will
	// activate and a `recover` call within it will catch
	// the panic.
	defer func() {
		if r := recover(); r != nil {
			// The return value of `recover` is the error raised in
			// the call to `panic`.
			fmt.Println("Recovered. Error:\n", r)
		}
	}()

	mayPanic()

	// This code will not run, because `mayPanic` panics.
	// The execution of `main` stops at the point of the
	// panic and resumes in the deferred closure.
	fmt.Println("After mayPanic()")
}

Summary

In this lab, you learned how to use the recover function to handle panics in Golang. By using recover, you can prevent your program from crashing and continue executing code.

Other Golang Tutorials you may like