GoLang Tutorial - Panic and Recover
Continued from Defer.
Panic is similar to throwing an exception. In other words, a panic is an exception in Go. A panic is caused either by a runtime error, or an explicit call to the built-in panic function.
When a panic is called normal execution of a goroutine is stopped immediately:
- When a program panics, it immediately starts to unwind the call stack.
- This continues until the program crashes and prints a stack trace, or until the built-in recover function is called.
A panic is caused either by a runtime error or an explicit call to the built-in panic function.
A program recovers from a panic by calling the built-in function recover, after which normal execution is resumed. Since all the functions ordinary code is skipped during a panic, the only chance we have to call recover is from within a deferred function as shown in the picture below.
Picture credit: Go: Recover from a panic
During normal operation the recover function returns nil:
When panic() is called, the recover function returns whatever we gave as argument (in this case, it's "Argh!") to the panic function:
Here is a sample for the panic and recover from Go's blog:
The code: defer-panic-recover.go
The function g() takes the int i, and panics if i is greater than 3, or else it calls itself with the argument i+1. The function f() defers a function that calls recover and prints the recovered value (if it is non-nil).
If we remove the deferred function from f() the panic is not recovered and reaches the top of the goroutine's call stack, terminating the program:
The code: defer-panic-recover-2.go
The output now looks like this:
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization