DEV Community 👩‍💻👨‍💻

Cover image for Go Course: Panic and Recover
Karan Pratap Singh
Karan Pratap Singh

Posted on • Originally published at karanpratapsingh.com

Go Course: Panic and Recover

So earlier, we learned that the idiomatic way of handling abnormal conditions in a Go program is using errors. While errors are sufficient for most cases, there are some situations where the program cannot continue.

In those cases, we can use the built-in panic function.

Panic

func panic(interface{})
Enter fullscreen mode Exit fullscreen mode

The panic is a built-in function that stops the normal execution of the current goroutine. When a function calls panic, the normal execution of the function stops immediately and the control is returned back to the caller. This is repeated until the program exits with the panic message and stack trace.

Note: We will discuss goroutines later in the course.

So, let's see how we can use the panic function.

package main

func main() {
    WillPanic()
}

func WillPanic() {
    panic("Woah")
}
Enter fullscreen mode Exit fullscreen mode

And if we run this, we can see panic in action.

$ go run main.go
panic: Woah

goroutine 1 [running]:
main.WillPanic(...)
        .../main.go:8
main.main()
        .../main.go:4 +0x38
exit status 2
Enter fullscreen mode Exit fullscreen mode

As expected, our program printed the panic message, followed by the stack trace, and then it was terminated.

So, the question is, what to do when an unexpected panic happens?

Recover

Well, it is possible to regain control of a panicking program using the built-in recover function, along with the defer keyword.

func recover() interface{}
Enter fullscreen mode Exit fullscreen mode

Let's try an example by creating a handlePanic function. And then, we can call it using defer.

package main

import "fmt"

func main() {
    WillPanic()
}

func handlePanic() {
    data := recover()
    fmt.Println("Recovered:", data)
}

func WillPanic() {
    defer handlePanic()

    panic("Woah")
}
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
Recovered: Woah
Enter fullscreen mode Exit fullscreen mode

As we can see, our panic was recovered and now our program can continue execution.

Lastly, I will mention that panic and recover can be considered similar to the try/catch idiom in other languages. But one important factor is that we should avoid panic and recover and use errors when possible.

If so, then this brings us to the question, when should we use panic?

Use Cases

There are two valid use cases for panic:

  • An unrecoverable error

Which can be a situation where the program cannot simply continue its execution.

For example, reading a configuration file which is important to start the program, as there is nothing else to do if the file read itself fails.

  • Developer error

This is the most common situation. For example, dereferencing a pointer when the value is nil will cause a panic.


This article is part of my open source Go Course available on Github.

GitHub logo karanpratapsingh / go-course

Master the fundamentals and advanced features of the Go programming language

Go Course

Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.

This course is also available on my website as well as on Educative.io

banner

Table of contents

What is Go?

Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.

It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed, and safety of a statically typed and compiled language with the ease…

Top comments (0)

🌚 Life is too short to browse without dark mode