SoFunction
Updated on 2025-03-05

Go error handling panic function and recover function use and exception capture method

Preface

We have talked about the error type to handle general errors. This article will describe using panic functions and recover functions to handle more extreme errors. Simply put, when the program encounters an unprocessable error or exception situation, it will be called.panicThe function raises aRuntime error,at this timeThe program will terminate execution and throw an error message. To avoid program crashes, you can userecoverFunctions comeCatch errorsand perform processing or resume execution of the program, usuallyrecoverThe function is written indeferIn the statement.

panic function

The panic function is a built-in function, and its structure is as follows:

func panic(v interface{})

This function accepts an interface type value, which is usually an error description. After calling the second function, an exception will be raised, which will abortCurrent program flow, and record the current exception information in the stack. This function can be called actively, or passively in the event of array outage, null pointer reference, etc.

We will make a small modification to the division example in the previous article and use panic for error handling:

package main

import (
   "fmt"
)

func divide(a, b int) int{
   if b == 0 {
      panic("division by zero")
   }
   return a / b
}

func main() {
   result := divide(10, 0)
   ("the result is ",result)
}

After running, the program exits directly and reportsdivision by zeromistake.

Recover function

Sometimes, we don’t want the panic function to exit directly and roughly. At this time, we can use the recover function to capture the pannic. Note that the recover function can only be written after the defer keyword!

Recover is also a built-in function, and its structure is as follows:

func recover() interface{}

It can catch the error reported by the panic function and return it using the interface type.

We use recover to modify the example again so that the program can continue to run instead of roughly exiting. The code is as follows:

package main

import (
   "fmt"
)

func divide(a, b int) int{
   defer func() {
      if err:=recover();err!=nil {
         ("runtime panic : %v\n", err)
      }
   }()
   if b == 0 {
      panic("division by zero")
   }
   return a / b
}

func main() {
   result := divide(10, 0)
   ("the result is ",result)
}

Summarize

This blog discusses how to use panic and recover to handle exceptions in Go. When the program encounters an unprocessable error, it can use the panic function to raise an exception and use the recover function to combine the defer keyword to recover from the exception.

This is the article about the use of panic functions and recover functions and methods for catching exceptions in Go error handling. For more related content on go exception catching panic and recover, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!