[Awesome Go] Use panic and recover to handle errors and exceptions - os.Exit - log.Fatal - panic - defer - recover - Programming Language - Golang (Go) - Exception Error Handling
panic(v interface{}) and recover() interface{}
In the Go language, an error is considered an expected result, but an exception is an unexpected result. An exception may indicate that there is a Bug in the program or other uncontrollable problems have occurred.
If an error is encountered during internal recursive code, an exception will be thrown to quickly jump out of the deeply nested function call, and then the outermost interface will capture panic through recover, and then return the corresponding error message.
panic(v interface{}) and recover() interface{}
Go language recommends using the recover
built-in function to turn internal exceptions into error handling, so that users can really care about business-related error handling.
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.
A panic is caused either by a runtime error or an explicit call to the built-in panic function.
Let’s see function panic(v interface{})
definition.
1 | // The panic built-in function stops normal execution of the current |
Let’s see function recover() interface{}
definition.
1 | // The recover built-in function allows a program to manage behavior of a panicking goroutine. Executing a call to recover inside a deferred function (but not any function called by it) stops the panicking sequence by restoring normal execution and retrieves the error value passed to the call of panic. If recover is called outside the deferred function it will not stop a panicking sequence. In this case, or when the goroutine is not panicking, or if the argument supplied to panic was nil, recover returns nil. Thus the return value from recover reports whether the goroutine is panicking. |
Key points:
-
Stops normal execution of the current function F and the current goroutine.
-
Any functions whose execution was deferred by the current function F and the current goroutine.
-
Then the current function F returns to its caller G.
-
the caller G behaves like a call to panic, terminating G’s execution and running any deferred functions, likes 1, 2, 3.
-
This continues until all functions in the executing goroutine have stopped, in reverse order.
-
The program is terminated and the error condition is reported, including the value of the argument to panic.
-
This termination sequence can be controlled by the built-in function recover.
Example: main.go
1 | // Use `panic` to terminate program and the error condition is reported, |
1 | go run main.go |
Key points about recover() interface{}
.
-
the built-in function recover only execute within the deferred function, otherwise recover will return
nil
-
Once the panic is captured by the recover, it will not continue to spread.
References
[1] Defer, Panic, and Recover - The Go Blog - https://blog.golang.org/defer-panic-and-recover